0% found this document useful (0 votes)
96 views2 pages

Comp9020 Cheatsheet Cref

Uploaded by

zx136152382
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views2 pages

Comp9020 Cheatsheet Cref

Uploaded by

zx136152382
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6

6
1

1
Literals (examples) Operators (decreasing precedence down and across)
C Reference Card

-
2004-06-21
integers (int)

4
123 -4 0xAf0C 057 () [] . -> Brackets, array, struct, pointer-struct
substitutable parameters shown in italics
3 3.14159265 1.29e-23 reals (double) ++ -- - ! * Incr/decrement, unary minus, logical

3
´x´ ´\t´ ´\033´ characters (char) & ~ sizeof NOT, pointer deref., address-of, 1’s
1

1
Compilation
″hello″ ″abc\″\n″ ″″ strings (char *) (typename) complement, size in bytes, cast 
6

6
gcc –flags program.c * / % + - Binary arithmetic operators
Character and string escapes
2

2
dcc –flags program.c (CSE labs only) << >> Bitwise left shift/right shift
9

9
symbol represents symbol represents < <= > >= Relational operators
–c Compile only, output to file.o
1

1
== != & (In)equality operators; bitwise AND
–o file
Executable output to file \t tab \ddd ASCII value (octal)
2

2
^ | Bitwise exclusive OR, inclusive OR
–g Generate debugging info for gdb \n newline \´ single quote
8

8
&& || ?: 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
6

6
typedef enum { FALSE, TRUE } Bool; expression ;
1

1
#include <libmodule.h>
typedef struct { { statements… }
-

-
#include ″usermodule.h″ char *key; if (expression) statement
4

4
#define NAME replacement-text int val; if (expression) statement else statement
3

3
#define NAME(args…) replacement-text } KeyValType;
switch (expression) {
1

1
type funcname(type param1, type param2 …); case constant : statements… break;
Program structure:
6

6
More types case constant : statements… break;
2

2
Header files: declarations only (#includes, #defines, function default : statements
9

9
prototypes) short (int) long (int, double) }
1

1
Implementation files: #includes, #defines, prototypes for unsigned (int, char)
while (expression) statement
2

2
local functions, function definitions Storage classes (common) for (initialiser; condition; increment) statement
8

8
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″;
6

6
auto break case char const continue default
int seq[MAX] = { 1, 2, 3 };
1

1
do double else entry enum extern float for
goto if int long register return short KeyValType keylist[] = {
-

-
signed sizeof static struct switch typedef
4

4
union unsigned void volatile while ″NSW″, 0, ″Vic″, 5 , ″Qld″, -1 };
3

3
1

1
6

6
2

2
9

9
6

6
1

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:

4
Parameter name implies type: c char isupper(c) islower(c) case testing
3 int c;

3
n int l long s string (char *) isalpha(c) isalnum(c) alpha(betic|numeric)
b buffer (char array) p pointer (void *)
1

1
isdigit(c) isxdigit(c) decimal or hex digit while ((c = getchar()) != EOF) {
d double fh file handle (FILE *)
6

6
isspace(c) isprint(c) white space, printable putchar(c); // or some other use of c
}
2

2
stdlib.h string.h
9

9
atoi(s) atof(s) string to int or double Read input a line at a time:
strlen(s) length (excluding ´\0´)
1

1
malloc(n) calloc(n) allocate n bytes strcpy(sd,ss) copy ss to sd, return sd char buf[BUFSIZ];
free(p) recycle memory
2

2
strcat(sd,ss) append ss to sd, return sd while(fgets(buf,BUFSIZ,stdin) != NULL)
exit(n) terminate with status n
8

8
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″,
6

6
fclose(fh) close file strspn(s,set) length of prefix of any in set argv[1]);
1

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
4

4
math.h (all parameters are double) Same, but file name is optional, default standard input:
fputc(c,fh) putchar(c) write char
3

3
fputs(s,fh) write line sin(d) cos(d) tan(d) trigonometry (radians) if (argc == 1)
1

1
fread(p,size,nel,fh) read into binary buffer, asin(d) acos(d) atan(d) inverse (radians) process(stdin);
= tan−1(y/x)
6

6
return number of elements read atan2(y,x) else {
sinh(d) cosh(d) tanh(d) hyperbolic // open fp as above
2

2
fwrite(p,size,nel,fh) write from binary buffer
Formatted output: exp(d) log(d) log10(d) exponential, logarithm process(fp);
9

9
fprintf(fh, format, list) formatted output to fh pow(x,y) sqrt(d) xy, square root }
1

1
printf(format, list) fmt output to stdout floor(d) ceil(d) integral bounds Print array of real numbers:
2

2
sprintf(b, format, list) formatted output to string fabs(d) fmod(x,y) absolute value, x % y for (i=0; i < MAX; i++)
8

8
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:
6

6
fscanf(fh, format, list) formatted input from fh for (sp=s; *sp != ´\0´; sp++)
1

1
scanf(format, list) fmt input from stdin len++;
sscanf(s, format, list) formatted input from string return len;
-

-
4

4
format codes similar to printf, list has addresses }
3

3
1

1
6

6
2

2
9

You might also like