1 hamks@dongguk.ac.kr (goal) (abstraction), (modularity), (interface) (efficient) (robust) C Unix C Unix (operating system) (network) (compiler) (machine architecture) 1 2 3 4 5 6 7 8 9 10
ANSI C Systems programming pointer (address) arithmetic all functions are call-by-value simple 2-level scope structure no I/O or memory mgmt facilities (provided by libraries) History BCPL B C K&R C ANSI C 1960 1970 1972 1978 1988 Add_Box_To_Picture (Box,Picture,Position) {............ Drawing_Program() {... Add_Box_to_Picture(B,P,Pos)...
.h.c
#ifndef STACK_INCLUDED #define STACK_INCLUDED typedef struct Stack_T *Stack_T; extern Stack_T Stack_new(void); extern int Stack_empty(Stack_T stk); extern void Stack_push(Stack_T stk, void *x); extern void *Stack_pop(Stack_T stk); extern void Stack_free(Stack_T *stk); /* NULL Stack_T Stack_T* Stack_pop. */ #endif Stack_T Stack_T Stack_ #ifndef STACK_INCLUDED #include <assert.h> #include <stdlib.h> #include "stack.h" #define T Stack_T struct T { void *val; T next; ; T Stack_new(void) { T stk = calloc(1, sizeof *stk); assert(stk); return stk; int Stack_empty(T stk) { assert(stk); return stk->next == NULL; void Stack_push(T stk, void *x) { T t = malloc(sizeof *t); assert(t); assert(stk); t->val = x; t->next = stk->next; stk->next = t; void *Stack_pop(T stk) { void *x; T s; assert(stk && stk->next); x = stk->next->val; s = stk->next; stk->next = stk->next->next; free(s); return x; void Stack_free(T *stk) { T s; assert(stk && *stk); for ( ; *stk; *stk = s) { s = (*stk)->next; free(*stk); #include <stdio.h> #include <stdlib.h> #include "stack.h" int main(int argc, char *argv[]) { int i; Stack_T s = Stack_new(); for (i = 1; i < argc; i++) Stack_push(s, argv[i]); while (!Stack_empty(s)) printf("%s\n", Stack_pop(s)); Stack_free(&s); return EXIT_SUCCESS;
assert(e) int Stack_empty(T stk){ assert(stk); return stk->next == NULL; assert.h (): #ifdef NDEBUG #define assert(e) ((void)0) #else #define assert(e) ((void)((e) (fprintf(stderr, \ "assertion failed: file %s, line %d\n", \ FILE, LINE ), abort(), 0))) #endif gcc -DNDEBUG foo.c... gcc -c stack.c -o stack.o ar -rs mylib.a stack.o gcc user.c mylib.a nm -s mylib.a
assert.h ctype.h errno.h float.h limits.h locale.h math.h setjmp.h signal.h stdarg.h stddef.h stdio.h stdlib.h string.h time.h assertions character mappings error numbers metrics for floating types metrics for integral types locale specifics math functions non-local jumps signal handling variable length argument lists standard definitions standard I/O standard library functions string functions date/time functions stdlib.h atof, atoi, strtod, rand, qsort, getenv, calloc, malloc, realloc, free, abort, exit,... string.h strcmp, strncmp, strcpy, strncpy strcat, strncat, strchr, strrchr, strlen,... memcpy, memmove, memcmp, memset, memchr ctype.h isdigit, isalpha, isspace, ispunct, isupper, islower, toupper, tolower,... math.h sin, cos, tan, asin, acos, atan, atan2, ceil, floor, fabs sinh, cosh, tanh, exp, log, log10, pow, sqrt, stdarg.h va_list, va_start, va_arg, va_end setjmp.h jmp_buf, setjmp, longjmp
stdio.h FILE* extern FILE *stdin, *stdout, *stderr; extern int fclose(file *); extern FILE *fopen(const char *, const char *); extern int fprintf(file *, const char *,...); extern int fscanf(file *, const char *,...); extern int printf(const char *,...); extern int scanf(const char *,...); extern int sprintf(char *, const char *,...); extern int sscanf(const char *, const char *,...); extern int fgetc(file *); extern char *fgets(char *, int, FILE *); extern int fputc(int, FILE *); extern int fputs(const char *, FILE *); extern int getc(file *); extern int getchar(void); extern char *gets(char *); extern int putc(int, FILE *); extern int putchar(int); extern int puts(const char *); extern int ungetc(int, FILE *); extern int feof(file *); getword, elementcount i arrayindex PI, MAXLINE printword, PrintWord, print_word Strset_T, Strset_add
alpha = angle(p1, p2, p3); beta = angle(p1, p2, p3); gamma = angle(p1, p2, p3); if else if else then else assert(0) continue break return i = i + 1; /* i 1 */
0 0xffffffff char *string = hello ; int i; char *f(void) { char *p; i = 8; p = malloc(i) ; return p; 0 0xffffffff 0 int i; char *f(void) { char *p; i = 8; p = malloc(i) ; return p; 0xffffffff
0 int i; char *f(void) { char *p; i = 8; p = malloc(i) ; return p; 0xffffffff