0% found this document useful (0 votes)
231 views

C Reference Card

This document provides a reference card for the C programming language including: 1) Common C data types and literals. 2) Operator precedence from highest to lowest. 3) Common declarations like variable declarations and struct definitions. 4) Common statements like if/else, while loops, and function calls. 5) Standard library functions from headers like stdio.h, stdlib.h, and string.h. 6) Examples of common programming patterns like reading input character by character.

Uploaded by

demorach
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
231 views

C Reference Card

This document provides a reference card for the C programming language including: 1) Common C data types and literals. 2) Operator precedence from highest to lowest. 3) Common declarations like variable declarations and struct definitions. 4) Common statements like if/else, while loops, and function calls. 5) Standard library functions from headers like stdio.h, stdlib.h, and string.h. 6) Examples of common programming patterns like reading input character by character.

Uploaded by

demorach
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Literals (examples) Operators (decreasing precedence down and across)

C Reference Card 2004-06-21


123 -4 0xAf0C 057 integers (int) () [] . -> Brackets, array, struct, pointer-struct
substitutable parameters shown in italics 3.14159265 1.29e-23 reals (double) ++ -- - ! * Incr/decrement, unary minus, logical
´x´ ´\t´ ´\033´ characters (char) & ~ sizeof NOT, pointer deref., address-of, 1’s
Compilation
″hello″ ″abc\″\n″ ″″ strings (char *) (typename) complement, size in bytes, cast 
gcc –flags program.c * / % + - Binary arithmetic operators
Character and string escapes
dcc –flags program.c (CSE labs only) << >> Bitwise left shift/right shift
symbol represents symbol represents < <= > >= Relational operators
–c Compile only, output to file.o
== != & (In)equality operators; bitwise AND
–o file
Executable output to file \t tab \ddd ASCII value (octal) ^ | Bitwise exclusive OR, inclusive OR
–g Generate debugging info for gdb \n newline \´ single quote
&& || ?: Logical AND and OR; conditional 
\r carriage-return \″ double quote
–Wall Turn on all warnings = += -= *= Assignment (with optional arithmetic
\0 null character \\ backslash
/= %= etc operation) 
Declarations (examples) , Comma (sequential) operator
Lexical structure, preprocessor
int i, length; Left-associative except for  (right associative)
/* comment */ char *str, buf[BUFSIZ], prev;
double x, values[MAX]; Statements
// comment to end of line
typedef enum { FALSE, TRUE } Bool; expression ;
#include <libmodule.h>
typedef struct { { statements… }
#include ″usermodule.h″ char *key; if (expression) statement
#define NAME replacement-text int val; if (expression) statement else statement
#define NAME(args…) replacement-text } KeyValType;
switch (expression) {
type funcname(type param1, type param2 …); case constant : statements… break;
Program structure:
More types case constant : statements… break;
Header files: declarations only (#includes, #defines, function default : statements
prototypes) short (int) long (int, double) }
Implementation files: #includes, #defines, prototypes for unsigned (int, char)
while (expression) statement
local functions, function definitions Storage classes (common) for (initialiser; condition; increment) statement
Main program file: as for implementation, must have main: static local to file, or var saved across function calls do statement while (expression);
int main(int argc, char **argv) extern accessible everywhere break; terminate loop or switch
Identifiers start with a letter, followed by any number of Initialisation (examples) continue; resume next iteration of loop
letters, digits or underscores return expr; return value from function
int c = 0; goto identifier; transfer to label (rare)
Identifiers starting with _ reserved for system use
char prev = ´\n´;
Reserved words (can’t use as identifiers): char *mssg = ″hello″;
auto break case char const continue default
do double else entry enum extern float for int seq[MAX] = { 1, 2, 3 };
goto if int long register return short KeyValType keylist[] = {
signed sizeof static struct switch typedef
union unsigned void volatile while ″NSW″, 0, ″Vic″, 5 , ″Qld″, -1 };
C library functions (and other objects) ctype.h Common programming patterns
toupper(c) tolower(c) case mapping Read input a character at a time:
Parameter name implies type: c char isupper(c) islower(c) case testing
n int l long s string (char *) int c;
isalpha(c) isalnum(c) alpha(betic|numeric)
b buffer (char array) p pointer (void *) isdigit(c) isxdigit(c) decimal or hex digit while ((c = getchar()) != EOF) {
d double fh file handle (FILE *) isspace(c) isprint(c) white space, printable putchar(c); // or some other use of c
stdlib.h }
string.h
atoi(s) atof(s) string to int or double Read input a line at a time:
strlen(s) length (excluding ´\0´)
malloc(n) calloc(n) allocate n bytes strcpy(sd,ss) copy ss to sd, return sd char buf[BUFSIZ];
free(p) recycle memory strcat(sd,ss) append ss to sd, return sd
exit(n) terminate with status n while(fgets(buf,BUFSIZ,stdin) != NULL)
strcmp(s1,s2) compare, return <0 ==0 >0 process_line(buf);
abs(n) labs(l) absolute value
strncpy(sd,ss,n) strncat(sd,ss,n)
Opening a file named on the command line:
stdio.h strncmp(s1,s2,n) max n chars processed
FILE *fp;
stdin stdout stderr FILE * variables strchr(s,c) return ptr to first c in s
BUFSIZ EOF NULL constants strrchr(s,c) return ptr to last c in s fp = fopen(argv[1], ″r″);
fopen(s,mode) open file, returns fh strstr(s,sp) return ptr to first sp in s if (fp == NULL) {
mode is one or more of ″r″, ″w″, ″a″ ″b″ ″+″ strpbrk(s,set) return ptr to first of any in set fprintf(stderr, ″can’t open %s\n″,
fclose(fh) close file strspn(s,set) length of prefix of any in set argv[1]);
exit(1);
fgetc(fh) getchar() read char, EOF if none strcspn(s,set) length of prefix all not in set }
fgets(b,n,fh) read line, NULL if none
math.h (all parameters are double) Same, but file name is optional, default standard input:
fputc(c,fh) putchar(c) write char
fputs(s,fh) write line sin(d) cos(d) tan(d) trigonometry (radians) if (argc == 1)
fread(p,size,nel,fh) read into binary buffer, asin(d) acos(d) atan(d) inverse (radians) process(stdin);
return number of elements read atan2(y,x) = tan−1(y/x) else {
fwrite(p,size,nel,fh) write from binary buffer sinh(d) cosh(d) tanh(d) hyperbolic // open fp as above
Formatted output: exp(d) log(d) log10(d) exponential, logarithm process(fp);
fprintf(fh, format, list) formatted output to fh pow(x,y) sqrt(d) xy, square root }
printf(format, list) fmt output to stdout floor(d) ceil(d) integral bounds Print array of real numbers:
sprintf(b, format, list) formatted output to string fabs(d) fmod(x,y) absolute value, x % y for (i=0; i < MAX; i++)
format items %width.precision code printf(″%14.6f\n″, sample[i]);
negative width left-justifies. code is one of
Function to find the length of a string:
d decimal o octal x hexadecimal
f fixed point g general e exponential (scientific) int mystrlen(char *s)
c character s string p pointer {
int len = 0;
% literal ´%´ character char *sp;
Formatted input:
fscanf(fh, format, list) formatted input from fh for (sp=s; *sp != ´\0´; sp++)
scanf(format, list) fmt input from stdin len++;
sscanf(s, format, list) formatted input from string return len;
format codes similar to printf, list has addresses }

You might also like