a late-in-season picking of finest bytes Statements Declarations
have been brought together to produce this boutique
int i , length ;
— C reference card — expression;
if (condition) statement [ else statement]
{ statements... }
char *str , buf [BUFSIZ], prev ;
double x , values [MAX];
while (condition) statement
Compilation typedef enum { FALSE, TRUE } Bool;
return [value]; return (optional) value from function typedef struct { char *key ; int val ; } keyval t;
dcc flags file.c
-c compile only, default output file.o Operators Initialisation
-o out output to out
decreasing precedence downwards left-to-right
int c = 0;
operators are left-associative char prev = '\n';
Lexical Structure & Preprocessor except cast, ternary, assignment char *msg = "hello";
int seq [MAX] = { 1, 2, 3 };
/* a comment, maybe over multiple lines */ () brackets [v] vth index . struct field keyval t keylist [] = {
// a comment to the end of the line -> struct*’s field (‘arrow’, ‘stab’) "NSW", 0, "Vic", 5, "Qld", -1 };
++ increment -- decrement - negate ! logical-not
#include <system-header.h>
#include "user-header.h" Character & String Escapes
* dereference & reference (‘address-of’)
#define symbol replacement-text
˜ bitwise-not (1s-complement) (typename) type cast \n line feed (“newline”) carriage return \r
* / % + - arithmetic << >> left/right bitshift \t horizontal tab escape \e
.h files: #defines, typedefs, function prototypes \' single quote double quote \"
< <= > >= relational operators == != (in)equality
.c files: #defines, structs, statics, function definitions; & bitwise-and | bitwise-or ˆ bitwise-xor \\ backslash null character \0
int main (int argc , char *argv []) && logical-and || logical-or ?: ternary \ddd octal ascii value hex ascii value \xdd
= += -= *= /= %= (arithmetic on) assignment
Identifiers , sequential comma
Identifiers start with a letter, followed by letters, digits, or un- sizeof (typename) - bytes required to hold typename
derscores. Identifiers starting with ‘ ’ are reserved for system sizeof variable - bytes required to hold variable
use. The following words are also reserved:
Literals
auto break case char const continue default
do double else enum extern float for goto integers (int): 123 -4 0xAf0C 057
if inline int long register restrict return reals (double): 3.14159265 1.29e-23
short signed sizeof static struct switch characters (char): 'x' 't' '\033'
typedef union unsigned void volatile while strings (char *): "hello" "abc\"\n" ""
Bool Complex Imaginary
The C Standard Library size t strspn (char *str , char *any ); // in stdio.h
size t strcspn (char *str , char *any );
only a limited, ‘interesting’ subset is listed here. length of prefix of any of any (not) in str #define EOF (-1)
type modifiers, notably const, have been omitted. special “end-of-file” return value
char *strsep (char **strp , char *sep );
consult the relevant man(1) or info(1) pages. find first of any of sep in *strp , writes nul FILE *stdin , *stdout , *stderr ;
returns original *strp , byte after sep in strp standard input/output/error
replaces old strtok FILE *fopen (char *filename , char *mode );
// in stdlib.h open file; return new ‘file handle’.
#define NULL ((void *)0) int fclose (FILE *fh );
// in ctype.h close a file; returns non-zero on error.
void *malloc (size t size );
void *calloc (size t number , size t size ); int toupper (int c ); int tolower (int c ); int fgetc (FILE *fh ); int getchar (void);
allocate size or (number * size ) bytes. make ascii c uppercase or lowercase return next character from fh , or EOF on eof/error.
calloc initialises allocated space to zero. getchar equivalent to fgetc (stdin )
int isupper (int c ); int islower (int c );
void free (void *obj ); int isalpha (int c ); int isalnum (int c ); char *fgets (char *s , int size , FILE *fh );
release allocated pointer obj , no-op if NULL. int isdigit (int c ); int isxdigit (int c ); read into s until eof, newline, or size bytes.
int isspace (int c ); int isprint (int c ); returns s , or NULL on error.
void exit (int status ); void abort ();
terminate the current program (ab)normally.
is ascii c upper/lowercase, alphabetic, alphanumeric, int fputc (int c , FILE *fh ); int putchar (int c );
returns status to the os or sends SIGABRT.
a digit, a hex digit, whitespace, or printable? write c to fh ; returns EOF on error.
putchar (k) equivalent to fputc (k, stdout )
int atoi (char *str );
int fputs (char *str , FILE *fh );
long strtol (char *str , char **end , int base ); // in math.h
int puts (char *str );
converts string str to an (long) int.
// compile and link -lm if not using dcc write str to fh ; returns EOF on error.
double atof (char *str );
puts (k) equivalent to fputs (k "\n", stdout )
converts string str to a double. double sin (double x ); double asin (double x );
int abs (int x ); double cos (double x ); double acos (double x ); int printf (char *fmt , ...);
double tan (double x ); double atan (double x ); int fprintf (FILE *fh , char *fmt , ...);
returns sin, sin−1 , cos, cos−1 , tan, tan−1 of x int sprintf (char *str , char *fmt , ...);
// in string.h print text per fmt to stdout , fh or str .
double atan2 (double y , double x ); formatting commands: "%m w.p c"
size t strlen (char *str ); returns tan−1 xy field width in w; < 0 left-justifies. double places in p.
the length of str without trailing nul. double exp (double x ); double log (double x ); code in c: decimal, octal, hexadecimal, char, string,
char *strcpy (char *dst , char *src ); double log10 (double x ); fixed-point, general, exp., pointer, literal %
size t strlcpy (char *dst , char *src , size t sz ); returns exp, loge , log10 of x size in m: long [long]; short [short], size t, ptrdiff t.
char *strcat (char *dst , char *src ); double pow (double x , double y ); arguments with matching types follow fmt
returns xy returns number of characters written, or EOF on error
int strcmp (char *s1 , char *s2 );
double sqrt int scanf (char *fmt , ...);
√(double x );
return < 0, = 0, > 0 if s1 <, =, > s2
returns x int fscanf (FILE *fh , char *fmt , ...);
char *strchr (char *str , int c );
int sscanf (char *str , char *fmt , ...);
char *strrchr (char *str , int c ); double floor (double x ); double ceil (double x ); parse text from stdout , fh or str per fmt .
points to first/last instance of c in str , or NULL returns bxc and dxe fmt is not exactly the same as printf formats.
char *strstr (char *haystack , char *needle ); double fabs (double x ); pointer arguments with matching types follow fmt
find first instance of string needle in haystack returns |x| returns number of fields matched, or -1 on error
char *strpbrk (char *str , char *any ); double fmod (double x , double y );
find first of any of any in str . returns x mod y
ASCII Table
Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
0 00 NUL '\0' (null) 32 20 Space 64 40 @ 96 60 `
1 01 SOH (start of header) 33 21 ! 65 41 A 97 61 a
2 02 STX (start of text) 34 22 " 66 42 B 98 62 b
3 03 ETX (end of text) 35 23 # 67 43 C 99 63 c
4 04 EOT (end of transmission) 36 24 $ 68 44 D 100 64 d
5 05 ENQ (enquiry) 37 25 % 69 45 E 101 65 e
6 06 ACK (acknowledgment) 38 26 & 70 46 F 102 66 f
7 07 BEL '\a' (bell) 39 27 ' 71 47 G 103 67 g
8 08 BS (backspace) 40 28 ( 72 48 H 104 68 h
9 09 TAB '\t' (horizontal tab) 41 29 ) 73 49 I 105 69 i
10 0A LF '\n' (line feed, new line) 42 2A * 74 4A J 106 6A j
11 0B VT '\v' (vertical tab) 43 2B + 75 4B K 107 6B k
12 0C FF '\f' (form feed) 44 2C , 76 4C L 108 6C l
13 0D CR '\r' (carriage return) 45 2D - 77 4D M 109 6D m
14 0E SO (shift out) 46 2E . 78 4E N 110 6E n
15 0F SI (shift in) 47 2F / 79 4F O 111 6F o
16 10 DLE (data link escape) 48 30 0 80 50 P 112 70 p
17 11 DC1 (device control 1) 49 31 1 81 51 Q 113 71 q
18 12 DC2 (device control 2) 50 32 2 82 52 R 114 72 r
19 13 DC3 (device control 3) 51 33 3 83 53 S 115 73 s
20 14 DC4 (device control 4) 52 34 4 84 54 T 116 74 t
21 15 NAK (negative ack) 53 35 5 85 55 U 117 75 u
22 16 SYN (synchronous idle) 54 36 6 86 56 V 118 76 v
23 17 ETB (end of trans. block) 55 37 7 87 57 W 119 77 w
24 18 CAN (cancel) 56 38 8 88 58 X 120 78 x
25 19 EM (end of medium) 57 39 9 89 59 Y 121 79 y
26 1A SUB (substitute) 58 3A : 90 5A Z 122 7A z
27 1B ESC (escape) 59 3B ; 91 5B [ 123 7B {
28 1C FS (file separator) 60 3C < 92 5C \ 124 7C |
29 1D GS (group separator) 61 3D = 93 5D ] 125 7D }
30 1E RS (record separator) 62 3E > 94 5E ˆ 126 7E ˜
31 1F US (unit separator) 63 3F ? 95 5F 127 7F DEL