C Reference Card (ANSI) Constants long (suffix)
Program Structure/Functions float (suffix)
L or l F or f e
0
0x or 0X
‘a’, ‘\ooo’, ‘\xhh’
\n, \r, \t, \b \\, \?, \’, \” “abc. . . de”
Flow of Control
statement terminator
block delimeters
exit from switch, while, do, for next iteration of while, do, for go to
label
return value from function Flow Constructions
if statement
type fnc(type1,…) type name
main() {
declarations
statements
}
type fnc(arg1,…) {
declarations statements return value;
}
/* */
main(int argc, char *argv[]) exit(arg )
C Preprocessor
include library file include user file replacement text replacement macro
function declarations external variable declarations main routine
local variable declarations
function definition
local variable declarations
exponential form
octal (prefix zero)
hexadecimal (prefix zero-ex) character constant (char, octal, hex) newline, cr, tab, backspace
special characters
string constant (ends with ‘\0’)
;
{}
break continue goto label label: return expr
statement
for (expr1; expr2; expr3)
statement
do statement while(expr );
switch (expr ) {
case const1: statement1 break; case const2: statement2 break; default: statement
}
undefine
quoted string in replace concatenate args and rescan conditional execution
is name defined, not defined? name defined?
line continuation char
#undef name };
# create structure
## member of structure from template
#if, #else, #elif, #endif #ifdef, #ifndef
struct tag name name.member pointer -> member
Data Types/Declarations
character (1 byte) integer
float (single precision) float (double precision) short (16 bit integer) long (32 bit integer) positive and negative only positive
pointer to int, float,. . . enumeration constant constant (unchanging) value declare external variable register variable
local to source file
no value
structure
create name by data type
size of an object (type is size_t) size of a data type (type is size_t)
Initialization
initialize variable
initialize array type initialize char string
single value, multiple type structure union
bit field with b bits member : b
Operators (grouped by precedence)
alphanumeric?
alphabetic?
control character?
decimal digit?
printing character (not incl space)? lower case letter?
printing character (incl space)?
printing char except space, letter, digit? space, formfeed, newline, cr, tab, vtab? upper case letter?
hexadecimal digit?
convert to lower case?
convert to upper case?
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)
defined(name ) \
member of pointed to structure
Example. (*p).x and p->x are the same
Pointers, Arrays & Structures
declare pointer to type type *name
while statement for statement do statement
switch statement
declare function returning pointer to type type *f() comments declare pointer to function returning type type (*pf)()
main with args generic pointer type terminate execution null pointer
object pointed to by pointer
void * NULL *pointer &name name [dim ]
address of ob ject
“filename ” multi-dim array #define name text Structures
name
#include
#include
name [dim 1 ][dim 2 ]. . . structure template
declaration of members
#define name (var ) text struct tag { Example. #define max(A,B) ((A)>(B) ? (A) : (B)) declarations
ANSI Standard Libraries
Character Class Tests
if (expr )
else if (expr ) statement else statement
while (expr )
statement
char structure member operator int structure pointer
name .member pointer ->member
++, —
+, -, !, ~ *pointer, &name (type) expr sizeof
*, /, %
+, –
<<, >>
>, >=, <, <= ==, !=
& ^ | && ||
char name []="string " expression evaluation separator
Unary operators, conditional expression and assignment oper-
float increment, decrement
double plus, minus, logical not, bitwise not
short indirection via pointer, address of object
long cast expression to type
*int, *float,... multiply, divide, modulus (remainder) enum add, subtract
const left, right shift [bit ops] extern
register comparisons
signed size of an ob ject unsigned
String Operations
static
void
struct
typedef typename sizeof object sizeof(type name )
comparisons bitwise and
bitwise exclusive or bitwise or (incl) logical and
logical or
conditional expression
length of s copy ct to s
up to n chars concatenate ct after s up to n chars
compare cs to ct only first n chars
pointer to first c in cs
pointer to last c in cs
copy n chars from ct to s
copy n chars from ct to s (may overlap) compare n chars of cs with ct
strlen(s)
strcpy(s,ct)
strncpy(s,ct,n)
strcat(s,ct)
strncat(s,ct,n)
strcmp(cs,ct)
strncmp(cs,ct,n)
strchr(cs,c)
strrchr(cs,c)
memcpy(s,ct,n)
memmove(s,ct,n)
memcmp(cs,ct,n)
memchr(cs,c,n)
memset(s,c,n)
type name=value
name []={value 1 ,. . . } assignment operators
expr ? expr 123
: expr +=, -=, *=, . . .
,
pointer to first c in first n chars of cs put c into first n chars of cs
ators group right to left; all others group left to right. 123
⃝c 1999 Joseph H. Silverman Permissions on back. v1.3
C Reference Card (ANSI)
Standard Utility Functions
Integer Type Limits
Input/Output
standard input stream standard output stream standard error stream end of file
get a character
print a character
print formatted data
print to string s
read formatted data
read from string s
read line to string s (< max chars) print string s
File I/O
declare file pointer
pointer to named file
absolute value of int n
absolute value of long n
quotient and remainder of ints n,d
abs(n)
labs(n)
div(n,d)
constants on a 32-bit Unix system.
modes: r (read), w (write), a (append)
allocate storage
change size of ob ject deallocate space
Array Functions
search array for key
sort array ascending order
malloc(size), calloc(nobj,size) realloc(pts,size)
free(ptr)
bsearch(key,array,n,size,cmp())
qsort(array,n,size,cmp())
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
radix of exponent rep
floating point rounding mode decimal digits of precision smallest x so 1.0 + x ̸= 1.0 number of digits in mantissa maximum floating point number maximum exponent
minimum floating point number minimum exponent
decimal digits of precision smallest x so 1.0 + x ̸= 1.0 number of digits in mantissa
max double floating point number maximum exponent
min double floating point number minimum exponent
(2) (6)
(10−5 )
(1037 )
(10−37 )
(10) (10−9 )
(1037 ) (10−37 )
get a character
write a character
write to file
read from file
close file
non-zero if error
non-zero if EOF
read line to string s (< max chars) write string s
getc(fp)
Codes for Formatted I/O: "%-+ 0w.pmc"
-
+
space
0
w p m
c
left justify
print with sign
print space if no sign pad with leading zeros min field width precision
conversion character:
arithmetic types representing times structure type for calendar time comps
clock_t,time_t
tm
long double
number of chars written g,G same as f or e,E depending on exponent
tm_sec
tm_min
tm_hour
tm_mday
tm_mon
tm_year
tm_wday
tm_yday
tm_isdst
seconds after minute minutes after hour hours since midnight day of month
months since January years since 1900
h short, l conversion character:
d,i integer
c single char f double
o octal x,X
p pointer n
access next unamed arg, update pointer call before exiting function
va_arg(name,type) va_end(name)
tp is a pointer to a structure of type tm Mathematical Functions
Arguments and returned values are double
long,
u unsigned
L s char string
days since Sunday
days since January 1 Daylight Savings Time flag
e,E exponential hexadecimal
convert local time to calendar time convert time in tp to string
convert calendar time in tp to local time convert calendar time to GMT
mktime(tp)
asctime(tp)
ctime(tp)
gmtime(tp)
localtime(tp)
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
convert calendar time to local time
format date and time info strftime(s,smax,”format “,tp)
stdin
stdout stderr
EOF getchar() putchar(chr )
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
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
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
(8) (127 or 255) (−128 or 0) (+32,767) (−32,768) (+2,147,483,647) (−2,147,483,648) (+127) (−128) (+32,767) (−32,768) (255) (65,535) (4,294,967,295) (65,536)
printf(“format “,arg 1 ,. . . ) sprintf(s,”format “,arg 1 ,. . . ) scanf(“format “,&name 1 ,. . . ) sscanf(s,”format “,&name 1 ,. . . )
pseudo-random integer [0,RAND_MAX] set random seed to n
terminate program execution
pass string s to system for execution Conversions
rand()
srand(n)
exit(status)
system(s)
atof(s)
atoi(s)
atol(s)
strtod(s,endp)
strtol(s,endp,b)
strtoul(s,endp,b)
gets(s,max)
puts(s)
FILE *fp fopen(“name “,”mode “)
convert string convert string convert string convert prefix convert prefix
s to double
s to integer
s to long
of s to double
of s (base b) to long
putc(chr ,fp) fprintf(fp ,”format “,arg
,. . . ) fscanf(fp,”format”,arg1,…)
fclose(fp ) ferror(fp ) feof(fp ) fgets(s,max,fp ) fputs(s,fp )
Time and Date Functions
processor time used by program clock() Example. clock()/CLOCKS_PER_SEC is time in seconds
current calendar time time()
time -time in seconds (double) difftime(time ,time ) 21 21
1
same, but unsigned long Storage Allocation
Float Type Limits
trig functions
inverse trig functions arctan(y/x)
hyperbolic trig functions exponentials & logs exponentials & logs (2 power) division & remainder
powers
rounding
sin(x), cos(x), tan(x) asin(x), acos(x), atan(x)
atan2(y,x) sinh(x), cosh(x), tanh(x)
exp(x), log(x), log10(x) ldexp(x,n), frexp(x,*e) modf(x,*ip), fmod(x,y)
pow(x,y), sqrt(x) ceil(x), floor(x), fabs(x)
May 1999 v1.3. Copyright ⃝c 1999 Joseph H. Silverman
Permission is granted to make and distribute copies of this card pro- vided 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. ⟨jhs@math.brown.edu⟩
456