Reference Card (ANSI) main with args function denition local variable declarations include library les include user les constant & macro denitions function declarations external variable declarations main routine local variable declarations ram Structure/Functions { e <lename > e "lename " name value (type 1 ,: : : ) me { ations ments (arg 1 ,: : : ) ations ments value ; n t argc, char *argv[]) ::: Types/Declarations r (1 byte) tants ngle precision) uble precision) 6 bit integer) bit integer) and negative itive to int, float, tion constant (unchanging) value external variable variable source le char int float double short long signed unsigned *int, *float,: : : enum const extern register static void struct typedef sizeof sizeof( ) typename object type name e ame by data type n object (type is size_t) data type (type is size_t) L l F f e 0 0x 0X a \ \x \n, \r, \t, \b \\, \?, \, \" "abc: : : de" type name =value type name []={value 1 ,: : : } char name []="string " ooo hh or or or x) x) tial form rex zero) mal (prex zero-ex) r constant r constant (octal) r constant (hexadecimal) cr, tab, backspace haracters nstant (ends with \0) alization variable array char string 1998 Joseph H. Silverman Permissions on back. v1.0 Operators ::: arithmetic operations +, -, *, / modulus (remainder) % comparisons ==, !=, >, >=, <, <= and, or, not [logical ops] &&, ||, ! cast expression to type (type ) expr increment, decrement ++, -and, or (incl), or (excl) [bit ops] &, |, ^ left, right shift, ones compl. [bit ops] <<, >>, ~ assignment operators +=, -=, *=, conditional expression expr 1 ? expr 2 : expr 3 object pointed to by pointer *pointer address of object &name structure member operator name .member structure pointer pointer ->member expression evaluation separator , Flow of Control statement terminator ; block delimeters { } comment delimeters /* */ exit from switch, while, do, for break next iteration of while, do, for continue go to goto label label label : return value from function return expr terminate execution exit(arg ) Flow Constructions (if/while/for/do/switch) if (expr ) statement else if (expr ) statement else statement while (expr ) statement for (expr 1 ; expr 2 ; expr 3 ) statement do statement while(expr ); expr const statement 1 break; const statement 2 break; statement switch ( ) { case 1: case 2: default: } Pointers & Arrays pointer name type name name dim ] name dim 1 ,: : : ] pointer ->member * & * [ [ void * object pointed to by pointer address of object name pointer declaration array multi-dim array generic pointer structure pointer type *name type *f() type (*pf)() Pointer Declarations pointer to type function f returning pointer to type pointer pf to function returning type Input/Output Standard I/O < <stdio.h> ::: ::: :::) :::) ) ) standard input stream stdin standard output stream stdout standard error stream stderr end of le EOF get a character getchar() print a character putchar(chr ) print formatted data printf("format ",arg 1 , print to string s sprintf(s,"format ",arg 1 , read formatted data scanf("format ",&name 1 , read from string s sscanf(s,"format ",&name 1 , read line to string s ( max chars) gets(s,max) print string s puts(s) File I/O c p w < structure template declaration of members w:pc ::: ::: declare le pointer FILE *fp pointer to named le fopen("name ","mode ") modes: r (read), w (write), a (append) get a character getc(fp ) write a character putc(chr ,fp ) write to le fprintf(fp ,"format ",arg 1 , ) read from le fscanf(fp ,"format ",arg 1 , ) close le fclose(fp ) non-zero if error ferror(fp ) non-zero if EOF feof(fp ) read line to string s ( max chars) fgets(s,max,fp ) write string s fputs(s,fp ) Codes for Formatted I/O: "%-+ 0 " left justify + print with sign space print space if no sign 0 pad with leading zeros min eld width precision conversion character: integer u unsigned single char s char string double e,E exponential octal x,X hexadecimal pointer n number of chars written same as f or e,E depending on exponent d,i c f o p g,G Structures tag { declarations struct }; b b create structure struct tag name member of structure from template name .member member of pointed to structure pointer -> member Example. (*p).x and p->x are the same one value, multiple type structure union bit eld with bits member : lename lename name text name var text Reference Card (ANSI) eprocessor ibrary le user le ment text ment macro mple. <errno.h> <setjmp.h> <stdlib.h> <ctype.h> <float.h> <signal.h> <string.h> <limits.h> <stdarg.h> <time.h> name name #include < > #include " " #define #define ( ) #define max(A,B) ((A)>(B) ? (A) : (B)) #undef # ## #if, #else, #elif, #endif #ifdef, #ifndef defined( ) \ tring in replace nate args and rescan nal execution dened, not dened? ned? inuation char <ctype.h> <math.h> <stdio.h> I Standard Libraries .h> .h> .h> acter Class Tests isalnum(c) isalpha(c) iscntrl(c) isdigit(c) isgraph(c) islower(c) isprint(c) ispunct(c) isspace(c) isupper(c) isxdigit(c) tolower(c) toupper(c) <string.h> meric? ic? character? digit? character (not incl space)? se letter? character (incl space)? char except space, letter, digit? rmfeed, newline, cr, tab, vtab? ase letter? mal digit? to lower case? to upper case? g Operations e strings, cs,ct are constant strings fs strlen(s) to s strcpy(s,ct) to n chars strncpy(s,ct,n) nate ct after s strcat(s,ct) to n chars strncat(s,ct,n) cs to ct strcmp(cs,ct) y rst n chars strncmp(cs,ct,n) to rst c in cs strchr(cs,c) to last c in cs strrchr(cs,c) hars from ct to s memcpy(s,ct,n) hars from ct to s (may overlap) memmove(s,ct,n) n chars of cs with ct memcmp(cs,ct,n) to rst c in rst n chars of cs memchr(cs,c,n) to rst n chars of cs memset(s,c,n) Standard Utility Functions <stdlib.h> absolute value of int n abs(n) absolute value of long n labs(n) quotient and remainder of ints n,d div(n,d) retursn structure with div_t.quot and div_t.rem quotient and remainder of longs n,d ldiv(n,d) returns structure with ldiv_t.quot and ldiv_t.rem pseudo-random integer [0,RAND_MAX] rand() set random seed to n srand(n) terminate program execution exit(status) pass string s to system for execution system(s) atof(s) atoi(s) atol(s) strtod(s,endp) strtol(s,endp,b) strtoul(s,endp,b) Conversions convert string s to double convert string s to integer convert string s to long convert prex of s to double convert prex of s (base b) to long same, but unsigned long calloc(nobj,size) malloc(size) realloc(pts,size) free(ptr) Storage Allocation allocate storage allocate storage change size of object deallocate space Array Functions <time.h> search array for key bsearch(key,array,n,size,cmp()) sort array ascending order qsort(array,n,size,cmp()) Time and Date Functions <stdarg.h> processor time used by program clock() Example. clock()/CLOCKS_PER_SEC is time in seconds current calendar time time() time2 -time1 in seconds (double) difftime(time2 ,time1 ) arithmetic types representing times clock_t,time_t structure type for calendar time comps tm tm_sec seconds after minute tm_min minutes after hour tm_hour hours since midnight tm_mday day of month tm_mon months since January tm_year years since 1900 tm_wday days since Sunday tm_yday days since January 1 tm_isdst Daylight Savings Time ag convert local time to calendar time mktime(tp) convert time in tp to string asctime(tp) convert calendar time in tp to local time ctime(tp) convert calendar time to GMT gmtime(tp) convert calendar time to local time localtime(tp) format date and time info strftime(s,smax,"format ",tp) tp is a pointer to a structure of type tm Variable Argument Lists declaration of pointer to arguments va_list name ; initialization of argument pointer va_start(name ,lastarg ) lastarg is last named parameter of the function access next unamed arg, update pointer va_arg(name ,type ) call before exiting function va_end(name ) Mathematical Functions y=x <math.h> <limits.h> arguments and returned values are double trig functions sin(x), cos(x), tan(x) inverse trig functions asin(x), acos(x), atan(x) arctan( ) atan2(y,x) hyperbolic trig functions sinh(x), cosh(x), tanh(x) exponentials & logs exp(x), log(x), log10(x) exponentials & logs (2 power) ldexp(x,n), frexp(x,*e) division & remainder modf(x,*ip), fmod(x,y) powers pow(x,y), sqrt(x) rounding ceil(x), floor(x), fabs(x) 8 0 +32767 -32767 +2147483647 -2147483647 +127 -127 +32767 -32767 255 65535 4294967295 65536 <float.h> bits in char max value of char min value of char max value of int min value of int max value of long min value of long max value of signed char min value of signed char max value of short min value of short max value of unsigned char max value of unsigned int max value of unsigned long max value of unsigned short Integer Type Limits CHAR_BIT CHAR_MAX CHAR_MIN INT_MAX INT_MIN LONG_MAX LONG_MIN SCHAR_MAX SCHAR_MIN SHRT_MAX SHRT_MIN UCHAR_MAX UINT_MAX ULONG_MAX USHRT_MAX 2 6 1E-5 1E+37 1E-37 10 1E-9 1E+37 1E-37 x x : : x x 6 6 : : radix of exponent rep oating point rounding mode decimal digits of precision smallest so 1 0 + = 1 0 number of digits in mantissa maximum oating point number maximum exponent minimum oating point number minimum exponent decimal digits of precision smallest so 1 0 + = 1 0 number of digits in mantissa max double oating point number maximum exponent min double oating point number minimum exponent Float Type Limits FLT_RADIX FLT_ROUNDS FLT_DIG FLT_EPSILON FLT_MANT_DIG FLT_MAX FLT_MAX_EXP FLT_MIN FLT_MIN_EXP DBL_DIG DBL_EPSILON DBL_MANT_DIG DBL_MAX DBL_MAX_EXP DBL_MIN DBL_MIN_EXP c 1998 Joseph H. Silverman July 1998 v1.0. Copyright Permission is granted to make and distribute copies of this card provided the copyright notice and this permission notice are preserved on all copies. Send comments and corrections to J.H. Silverman, Math. Dept., Brown Univ., Providence, RI 02912 USA. hjhs@math.brown.edui