Standard C89 Reference
Standard C89 Reference
Standard C89 Reference
STATEMENT
DESCRIPTION
{ local_var_decl
statement
... }
break;
continue;
do
statement
while (expr);
expr;
for (e1;e2;e3)
statement
goto label;
if (expr)
statement
if (expr)
statement1
else
statement2
;
return expr;
Block.
The local_var_decl (local variable declarations) is optional.
Terminates execution of for, while, do, or
switch.
Skips statement that follow in a do, for, or while;
then continues executing the loop.
Executes statement until expr is FALSE;
statement is executed at least once.
Evaluates expr ; discards result.
Evaluates expr e1 once; then repeatedly evaluates
e2, statement, and e3 (in that order) until e2 is
FALSE; eg: for (i=1; i<=10,/; ++i)
...; note that statement will not be executed if
e2 is FALSE on first evaluation; e1 ,e2 and e3 are
optional; e2=1 assumed when omitted.
Branches to statement preceded by label, which
must be in same function as the goto. eg.:
int Fn(void) {
... goto write; ...
write: print("here am I"); ...}
If expr is TRUE, then executes statement;
otherwise skips it.
If expr is TRUE, then executes statement1;
otherwise executes statement2.
OPERAT ORS
OPERATOR
octal int
'
'a' '\n'
"
"hello" ""
f,F (1) 7.2f 2.e-15F -1E9f .5F
(1) 7.2 2.e-15 -1E9 .5
l,L (1) 7.2l 2.e-15l -1E9L .5L
(2) red january monday
17 -5 0,/
l,L (3) 251l 10,/0,/L
u,U
17u 5U 0,/u 65535u
0,/
0,/xFF 0,/Xff 0,/xA0,/0,/
x,0,/
0,/l
X
0,/
0,/777 0,/10,/0,/U 0,/
573ul
NOTES:
1. Decimal point and/or scientific notation.
2. Identifiers previously declared for an enumerated type; value treated as int.
3. Or any int too large for normal int
T YPE QUALIFIERS
const
volatile
Postincrement
Postdecrement
Array element ref
Function call
Struct member ref
Ptr to struct member
Size in bytes
Preincrement
Predecrement
Address of
Ptr indirection
Unary plus
Unary minus
Bitwise NOT
Logical negation
Type conversion / casting
Multiplication
Division
Modulus
Addition
Subtraction
Left shift
Right shift
Less than
Less than or equal to
Greater than
Greater than or eq to
Equal to
Not equal to
Bitwise AND
Bitwise XOR
Bitwise OR
Logical AND
Logical OR
Conditional operator
++
-[ ]
( )
.
->
sizeof
++
-&
*
+
~
!
(type)
*
/
%
+
<<
>>
<
<=
>
>=
==
!=
&
^
|
&&
||
? :
EXPRESSIONS
EXAMPLE
ASSOCIATION
ptr++
count-values [10,/]
sqrt (x)
child.name
child_ptr->name
sizeof child
++ptr
--count
&x
*ptr
+a
-a
~0,/77
! ready
(float) total/n
i * j
i / j
i % j
value + i
x - 10,/0,/
byte << 4
i >> 2
i < 10,/0,/
i <= j
i > 0,/
count >= 90,/
result == 0,/
c != EOF
word & 0,/77
word1 ^ word2
word | bits
j>0,/ && j<10,/
i>80,/ || ready
a>b ? a : b
If a greater than b then
expr=a else b
count += 2
It is equal to
count=count+2
DESCRIPTION
#if expr
...
#endif
#if expr
...
#else
...
#endif
#ifdef id
...
#endif
#ifndef id
...
#endif
#line n "file"
NOTES:
Preprocessor statements can be continued over multiple lines provided each line to
be continued ends with a backslash character (\). Statements can also be nested.
ST ORAGE CLASSES
STORAGE
CLASS
static
extern
auto
register
(omitted)
DECLARED
outside fn
inside fn/b
outside fn
inside fn/b
inside fn/b
inside fn/b
outside fn
CAN BE
REFERENCED
anywhere in file
inside fn/b
anywhere in file
inside fn/b
inside fn/b
inside fn/b
anywhere in file or
other files with ext.
declaration
inside fn/b
INIT
WITH
constant expr
constant expr
constant expr
cannot be init
any expr
any expr
constant expr
PREPROCESSOR ST AT EMENT S
STATEMENT
An expression is one or more terms and zero or more operators. A term can be
- name (function or data object)
- constant
- sizeof(type)
- (expr)
An expression is a constant expression if each term is a constant.
ARRAYS
= *= /= Assignment operators
%= += -=
&= ^= |=
<<= >>=
,
Comma operator
i=10,/ , j=0,/
NOTES:
Operators are listed in decreasing order of precedence.
Operators in the same box have the same precedence.
Associativity determines: grouping; order of evaluation for operands with
the same precedence:
( eg: a = b = c; is grouped right-to-left, as: a = (b = c); ).
CONST ANT S
char
char string
float
double
long double
enumeration
int
long int
unsigned int
hex integer
DESCRIPTION
NOTES
1
1
2
2
3
3,4,6
5
inside fn/b
any expr
3,6
NOTES:
1. Init at start of program execution; default is zero.
2. Variable must be defined in only one place w/o extern.
3. Variable is init each time fn/b is entered; no default value.
4. Register assignment not guaranteed; restricted (implementation dependent)
types can be assigned to registers. & (addr. of) operator cannot be applied.
5. Variable can be declared in only one place; initialized at start of program
execution; default is zero.
6. Defaults to auto.
} variable_list;
Each member declaration is a type followed by one or more member names;
variable_list (optional) declares variables of the particular union type. If
uname is supplied, then variables can also later be declared using the format:
union uname variable_list;
NOTE: unions cannot be initalized.
szandi@hit.bme.hu
same as d
pointer (same as in printf), arg type is void **
store number of chars have been matched, arg is int * , no input
store number of chars have been matched, arg is short * , no input
store number of chars have been matched, arg is long * , no input
single character, arg is char[ ]
string of chars terminated by a white-space character, arg is char[ ]
% itself
string of chars terminated by any char not enclosed between the [ and ];
if first char in brackets is ^, then following chars are string terminators
instead.
NOTES:
A scan function returns when:
It reaches the terminating null in the format string.
It cannot obtain additional input characters to scan.
A conversion fails.
Any chars in format string not preceded by % will literally match chars on input
(e.g. scanf("value=%d",&ival); will match chars "value=" on
input, followed by an integer which will be read and stored in ival.
Whitespace in format string matches the longest possible sequence of the zero
or more whitespace characters on input.
EXAMPLE:
sscanf("12Free of charge 21",
"%X%c%*[^ab]%2s%d",&i,&c,text,&j);
will return 3 and i=30,/3, c='r', text="ar"; j remains unchanged.
29-Jun-16
Backspace (BS)
Form feed (FF)
\n
\r
\t
\v
\a
Newline (NL)
Carriage return (CR)
Horizontal tab (HT)
Vertical tab (VT)
Bell (BEL)
\\
Backslash (\)
\nnn Octal character value (n: 0,/7)
\xhh Hexadecimal character value
(h: 0,/-9,a-f,A-F)
\"
Double quote (")
\'
Single quote (')
\?
Question mark (?)
int tolower(c)
int toupper(c)
Data conversion
double atof(s)
int atoi(s)
long atol(s)
double
strtod(s1,*s2)
long
strtol(s1,*s2,n)
convert c to lowercase
convert c to uppercase
unsigned long
strtoul(s1,*s2,n)
File handling and input/output
void clearerr(f)
int fclose(f)
int feof(f)
int ferror(f)
int fflush(f)
int fgetc(f)
int fgetpos(f,*fl)
char *fgets(s,n,f)
ASCII to
strtol)
FILE *fopen(s1,s2)
int fprintf(f,s,)
int fputc(c,f)
int fputs(s,f)
size_t fread
(v,su1,su2,f)
FILE
*freopen(s1,s2,f)
int fscanf(f,s,)
int fseek(f,l,n)
int fsetpos(f,*fl)
long ftell(f)
size_t fwrite(v,su1,
su2, f)
int getc(f)
int getchar()
char *gets(s)
void perror(s)
int printf(s,)
int putc(c,f)
int putchar(c)
int puts(s)
int remove(s)
int rename(s1,s2)
void rewind(f)
int scanf(s,)
void setbuf(f,s)
int
setvbuf(f,s,n,su)
int
sprintf(s1,s2,)
int sscanf(s1,s2,)
FILE *tmpfile()
char *tmpnam(s)
int ungetc(c,f)
int vfprintf(f,s,ap)
int vprintf(s,ap)
stdlib.h
ASCII to double conversion /HUGE_VAL,0,//
ASCII to int conversion
ASCII to long conversion
ASCII to double conversion; on return, *s2
points to char in s1 that terminated the scan/0,//
ASCII to long conversion, base n; on return, *s2
points to char in s1 that terminated the scan /0,/
stdio.h
reset error (incl. EOF) on file
close file /EOF/ (0,/)
TRUE if end-of-file on f
TRUE if I/O error on f
write buffered output to f /EOF/ (0,/)
read next char from f /EOF/
get the file position indicator to fl/TRUE/(0,/)
read n-1 chars from f unless newline or end-offile reached; newline is stored in s if read /NULL/
open file s1, mode s2: "w"=write, "r"=read,
"a"=append, "b"=binary, "+"=update /NULL/
write args to f using format s (see printf)
write c to f ; rtn c /EOF/
write s to f /EOF/ (0,/)
read su2 data items from f into v; su1 is
number bytes of each item /0,// (bytes read/su1)
close f and open s1 with mode s2 (see
fopen)
read args from f using format s (see scanf)
position file pointer; if n=SEEK_SET, l is offset
from beginning; if n=SEEK_CUR, from current
pos.; if n=SEEK_END, from end of file /TRUE/ (0,/)
sets the file position to fl (0,/) /TRUE/
current offset from the beginning of the file /-1L/
write su2 data items to f from v; su1 is number
of bytes of each item /0,// (bytes written/su1)
read next char from f /EOF/
read next char from stdin /EOF/
read chars into s from stdin until newline or
eof reached; newline not stored /NULL/
write s followed by descr. of last err. to stderr
write args to stdout per format s; return
number of characters written /<0,//
write c to f ; rtn c /EOF/
call fputc(c,stdout)
write s and newline to stdout /EOF/ (0,/)
removes the file named s (0,/) /TRUE/
rename the file named s1 to file s2 (0,/) /-1/
rewind f; calls fseek(f,0,/L,SEEK_SET)
read args from stdin per format s; return
number of values read or EOF
if s<>NULL calls setvbuf(f,s,_IOFBF,BUFSIZ)
otherwise calls setvbuf(f,NULL,_IONBF,BUFSIZ)
sets buffering mode for f, the buffer is s with
size su, n must be one of _IOFBF (full
buffering), _IOLBF (line buffering), _IONBF (no
buffering) (0,/) /TRUE/
write args to buffer s1 per format s2 (see
printf)
read args from s1 per format s2; (see scanf)
create temporary file, open with "wb+" mode;
return ptr to it /NULL/
generate temporary file name; place result in s if
s<>NULL (L_tmpnam size buffer); rtn ptr to name
insert c back into file f (as c wasn't read) /EOF/
see vprintf and fprintf
same as printf with variable argument list ap;
va_start must be called before and va_end
after the function
see vprintf and sprintf
int
vsprintf(s1,s2,ap)
Math
math.h,stdlib.h(*)
int errno (errno.h) detects range error (ERANGE) and domain error (EDOM).
int abs(n)
* absolute value of n
double acos(d)
arccosine of d /0,// [0,/,]
double asin(d)
arcsine of d /0,// [-/2,+/2]
double atan(d)
arctangent of d [-/2,+/2]
double atan2(d1,d2) arctangent of d1/d2 [-,+]
double ceil(d)
smallest integer not less than d
double cos(d)
cosine of d (d in radians)
double cosh(d)
hiperbolic cosine of d
div_t div(n1,n2) * computes the quotient (.quot) and remainder
(.rem) of division n1/n2
double exp(d)
e to the d-th power /HUGE_VAL/
double fabs(d)
absolute value of d
double floor(d)
largest integer not greater than d
double fmod(d1,d2) d1 modulo d2
double frexp(d,*n) returns x in interval [,1), and d=x*2n
long labs(l)
* absolute value of l
double ldexp(d,n)
d*2n
ldiv_t ldiv(l1,l2) * computes the quotient (.quot) and remainder
(.rem) of division l1/l2
double log(d)
natural log of d /0,//
double log10,/(d)
log base 10,/ of d /0,//
double modf(d1,*d2) rtn x such that d1=x+d2, x in [0,1), d2 integer
double pow(d1,d2)
d1 to the d2-th power /0,/,HUGE_VAL/
int rand()
* random number in range [0,/,RAND_MAX]
double sin(d)
sine of d (d in radians)
double sinh(d)
hyperbolic sine of d
double sqrt(d)
square root of d /0,//
void srand(u)
* reset random number generator to u
double tan(d)
tangent of d (radians) /HUGE_VAL/
double tanh(d)
hyperbolic tangent of d
Memory allocation and manipulation
string.h,stdlib.h(*)
void *calloc(su1,
allocate space for su1 elements; each su2
su2)
* bytes large and set to 0,/ /NULL/
void free(v)
* free block of space pointed to by v
void *malloc(su) * allocate su bytes and return ptr to it /NULL/
void
return ptr in v of 1st incident of c, looking at su
*memchr(v,c,su)
unsigned chars at most, or NULL if not found
int
rtn <0,/, =0,/, >0,/ if v1 is lexicographically
memcmp(v1,v2,su) <,= or >v2, comparing up to su unsigned
chars
void
copy su chars from v2 to v1 (v1, v2 should not
*memcpy(v1,v2,su) overlap); return v1
void *memmove
copy su chars from v2 to v1 (v1, v2 can
(v1,v2,su)
overlap); return v1
void
set su unsigned chars ptd to by v
*memset(v,c,su)
to value c; return v
void *realloc(v,su) change the size of block v to su and returns ptr
* to it /NULL/
Program contol
setjmp.h,stdlib.h(*)
void assert(iexpr) if NDEBUG is not defined and iexpr is FALSE
then write a diagnostic message to stderr and
calls abort(); use assert.h header
void abort()
* couse abnormal program termination
int atexit(void
register func to be called by exit (0,/) /TRUE/
(*func)(void)) *
void exit(n)
* terminate execution, returning exit status n
char *getenv(s)
* rtn ptr to value of environment name s /NULL/
void longjmp
restore environment from env; causes setjmp
(jmp_buf env,n)
to return n if supplied or 1 if n=0,/
int setjmp(jmp_buf save stack environment in env; (0,/) (see
env)
longjmp)
int system(s)
* execute s as if it were typed at terminal; returns
exit status /-1/
Searching and sorting
stdlib.h
void *bsearch(void binary search in array base (su1 elements,
*key, void *base, each su2 bytes large), using function cmp for
su1, su2,
comparison; cmp must return negativ if ck<ce,
int (*cmp)(void
0,/ if ck=ce, positiv if ck>ce
*ck, void *ce))
void qsort (void
quick sort of array base (su1 elements, each
*base, su1, su2, su2 bytes large), using function cmp for
int (*cmp)(void
comparison; (for cmp see bsearch)
*ck, void *ce))
String manipulation
string.h
char *strcat(s1,s2) concatenate s2 to end of s1; rtn s1
char *strchr(s,c)
return ptr to 1st occurence of c in s /NULL/
int strcmp(s1,s2)
compare s1 and s2; returns <0,/, 0,/, >0,/ if
s1 lexicographically <s2, =s2, >s2
char *strcpy(s1,s2) copy s2 to s1; rtn s1
size_t
search the first s1[i] that equals any element
strcspn(s1,s2)
of s2; rtn i
char *strerror(n)
return a pointer to string that message
corrensponds to errorcode n
size_t strlen(s)
length of s (not incl. NULL)
char *strncat
concatenate at most su chars from s2 to end of
(s1,s2,su)
s1; rtn s1
int
compare at most su chars from s1 to s2; (see
strncmp(s1,s2,su) strcmp)
char
copy at most su chars from s2 to s1; if s2 is
*strncpy(s1,s2,su) shorter than su, null bytes are appended; rtn s1
char
searches the first s1[i] that equals any
*strpbrk(s1,s2)
element of s2; return &s1[i]
char *strrchr(s,c) return pointer to last occurence of c in s /NULL/
size_t
search the first s1[i] that equals none of the
strspn(s1,s2)
element of s2; rtn i
char *strstr(s1,s2) search the first substring in s1 that matches s2
char *strtok(s1,s2) break s1 into tokens delimited by s2; from the
second call s1=NULL; s2 may different from call
to call; return the ptr to token or NULL
Time
time.h
char *asctime(*tm) convert tm struct to string; rtn ptr to it
clock_t clock()
CPU time in 1.0,//CLOCKS_PER_SEC seconds
since program startup /-1/
char *ctime(*tl)
convert time ptd to by tl to string; rtn ptr to it
double
difference tl1-tl2 in seconds
difftime(tl1,tl2)
struct tm
convert time pointed to by tl to Universal Time
*gmtime(*tl)
Coordinated (UTC) (formerly GMT)
struct tm
convert time pointed to by tl to local time
*localtime(*tl)
time_t mktime
alters tptr to represent an equivalent encoded
(struct tm *tptr) local time /-1/
size_t strftime(
write tptr to buffer s1 per format s2; buffer
s1, su, s2,
size is su; rtn number of characters stored /0,//
struct tm *tptr)
time_t time(*tl)
returns time & date in seconds; if tl<>NULL,
time is stored in *tl; convert time returned with
ctime, localtime or gmtime /-1/
Variable-type and number of arguments
stdarg.h
type
get next argument; ap must be initialized by
va_arg(ap,type)
va_start; the argument type must be type
void va_end(ap)
end variable argument list
void
start variable argument list; pN is the parameter
va_start(ap,pN)
just before the (...) in the function prototype
COMMAND LINE ARGUMENT S
Arguments typed in on the command line when a program is executed are
passed to the program through argc and argv.
argc is a count of the number of arguments +1;
argv is an array of character pointers that point to each argument.
argv[0,/] points to the name of the program executed.
argv[argc] equal NULL pointer.
Use sscanf to convert arguments stored in argv to other data types. For
example:
check phone 35.79
starts execution of a program called check, with :
argc = 3
argv[0,/] = "check"
argv[2] = "35.79"
argv[1] = "phone"
argv[3] = NULL
To convert number in argv[2], use sscanf. For example :
int main (int argc, char *argv[])
{ float amount;
sscanf (argv[2],"%f",&amount); }