Show Menu
Cheatography

C Cheat Sheet (DRAFT) by

C programming reference

This is a draft cheat sheet. It is a work in progress and is not finished yet.

i/o

#include <st­dio.h>
TAGS
"­­r" / "­­rb­"
Read existing text/b­­inary file.
"­­w" / "­­wb­"
Write new/over existing text/b­­inary file.
"­­a" / "­­ab­"
Write new/append to existing text/b­­inary file.
"­­r+­" / "­­r+­b­" / "­­rb­+­"
Read and write existing text/b­­inary file.
"­­w+­" / "­­w+­b­" / "­­wb­+­"
Read and write new/over existing text/b­­inary file.
"­­a+­" / "­­a+­b­" / "­­ab­+­"
Read and write new/append to existing text/b­­inary file.
RANDOM­-ACCESS
fread(void *buf, sizeof­­(e­l­e­ment), number, fptr)
Reads a number of elements from fptr to array *ptr. (safe)
fwrite­­(void *buf, sizeof­­(e­l­e­ment), number, fptr)
Writes a number of elements to file fptr from array *ptr.
SEQUENCIAL
fscanf­­(fptr, format, [...])
Same as scanf with additional file pointer parameter. (unsafe)
fprint­­f(­fptr, format, [...])
Same as printf with additional file pointer parameter.
fgets(­­st­r­Name, length, fptr);
sscanf­­(s­t­r­Name, "­­%d­", &x);
Uses fgets to limit the input length, then uses sscanf to read the resulting string in place of scanf. (safe)
NAVIGATION
fseek(­­fptr, offset, origin);
Sets current file position. Returns false is succes­­sful, true otherwise. The offset is a long integer type.
ftell(­­fptr)
Return current file position as a long integer.
SEEK_SET
Beginning of file.
SEEK_CUR
Current position in file.
SEEK_END
End of file.
feof(fptr)
Tests end-of­­-file indicator.

read line one by one

#include <st­­di­o.h>

FILE *h;
char line[100];

h = fopen(­­"­f­il­­ena­­me­", "­­rb­");
if (h == NULL) {
 ­ ­ ­  exit(1);
}
while (fgets­­(line, sizeof line, h)) {
 ­ ­ ­ / deal with line /
}

/ if needed test why last read failed /

if (feof(h) || ferror(h)) / whatever /;
fclose(h);

makefile

RULES
all: <ru­le>...
executes all specified rules
clean:
    rm <fi­le>...
removes specified files
.c to .o
<fi­len­ame­>.o: <fi­len­ame­>.c
FLAGS
$@
current target
$^
all sources for $@
$<
leftmost source for $@
 

memory

#include <st­dli­b.h>
malloc­(si­zeo­f(type) * length );
allocates memory, returns location.
reallo­c(p­trName, size);
reallo­cates memory, returns location.
free(p­trN­ame);
decall­ocates memory.

operators

BIT
a << b;
shift a to the left for b bits
a >> b;
shift a to the right for b bits
a & b;
bitwise and
a ^ b;
bitwise xor
a | b;
bitwise or
~a
bitwise complement

structs

BITFIELD
struct­{char a:4, b:4} x;
Declares x with two members a and b , both four bits in size (0 to 15.)
ENUM
enum bool { false, true };
A custom data type bool with two possible states: false or true .

j

 

character

#include <ct­­yp­e.h>
tolowe­r(char)
lowercase modifi­cation
touppe­r(char)
lowercase modifi­cation
isalph­a(char)
alphabet check
islowe­r(char)
lowercase check
isuppe­r(char)
uppercase check
isnumb­er(­char)
number check
isblan­k(char)
whitespace check

string

#include <st­rin­g.h>
DEFAULT
strlen(a)
return length
strcpy(a, b)
copy a to b
strcat(a, b)
concat­enate a to b
strcmp(a, b)
compares a to b (FALSE IF SAME)
strstr(a, b)
finds first str b in a, returns ptr.
strchr(a, b)
finds first char b in a, returns ptr.
strrch­r(a,b)
finds last char b in a, return ptr.
strpbr­k(a,b)
finds first char in a that's also in b, return ptr.
strspn(a, b)
returns range from a to char from b
strcspn(a, b)
returns range from a to char NOT from b
MEMORY