C Cheat Sheet
Last updated
Hello World & program structure
Every C program starts at main and returns an int.
| Element | Code |
|---|---|
| Include a header | #include <stdio.h> |
| Entry point | int main(void) { ... } |
| With command-line args | int main(int argc, char *argv[]) { ... } |
| Print a line | printf("Hello, World!\n"); |
| Return success | return 0; |
| Single-line comment | // comment |
| Block comment | /* comment */ |
Data types
Sizes are platform-dependent; the minimums below are guaranteed by the standard.
| Type | Description |
|---|---|
int | Integer, at least 16 bits (usually 32) |
unsigned int | Non-negative integer |
long / long long | Wider integers (at least 32 / 64 bits) |
float / double | Single / double precision floating point |
char | Single byte, also used for characters |
_Bool (bool via <stdbool.h>) | true or false |
size_t | Unsigned type for sizes and indices |
void | No type - used for functions and generic pointers |
printf & scanf format specifiers
Placeholders that match the type of each argument.
| Specifier | Matches |
|---|---|
%d / %i | Signed int |
%u | Unsigned int |
%ld / %lld | long / long long |
%f | double (and float in printf) |
%.2f | double with 2 decimal places |
%c | Single char |
%s | String (char *) |
%p | Pointer address |
%x | Unsigned int in hexadecimal |
%zu | size_t |
Operators
| Operator | Meaning |
|---|---|
+ - * / % | Arithmetic (% is integer remainder) |
== != | Equal / not equal |
< > <= >= | Comparison |
&& || ! | Logical and / or / not |
& | ^ ~ << >> | Bitwise and, or, xor, not, shifts |
++ -- | Increment / decrement |
+= -= *= /= | Compound assignment |
cond ? a : b | Ternary conditional |
sizeof(x) | Size of a type or variable in bytes |
Control flow
| Statement | Syntax |
|---|---|
| If / else | if (x > 0) { ... } else { ... } |
| Else if | else if (x == 0) { ... } |
| Switch | switch (n) { case 1: ...; break; default: ...; } |
| While loop | while (i < n) { ... } |
| Do-while loop | do { ... } while (i < n); |
| For loop | for (int i = 0; i < n; i++) { ... } |
| Break / continue | break; exits a loop, continue; skips to next iteration |
| Goto (rare) | goto label; ... label: |
Functions
Declare before use, or add a prototype at the top of the file.
| Operation | Syntax |
|---|---|
| Define a function | int add(int a, int b) { return a + b; } |
| Prototype (declaration) | int add(int a, int b); |
| No return value | void greet(void) { ... } |
| No parameters | int rand_seed(void) { ... } |
| Pass by pointer (mutate) | void inc(int *p) { (*p)++; } |
| Pass an array | int sum(int arr[], int n) { ... } |
| Static (file-local) | static int helper(void) { ... } |
Pointers & memory
A pointer holds an address. Heap memory must be freed manually.
| Operation | Syntax |
|---|---|
| Declare a pointer | int *p; |
| Address-of | p = &x; |
| Dereference (read/write) | int y = *p; / *p = 10; |
| Null pointer | int *p = NULL; |
| Allocate on the heap | int *a = malloc(n * sizeof(int)); |
| Allocate + zero | int *a = calloc(n, sizeof(int)); |
| Resize an allocation | a = realloc(a, newSize); |
| Free heap memory | free(a); |
| Pointer arithmetic | *(a + i) is the same as a[i] |
Arrays & strings
Strings in C are char arrays terminated by \0.
| Operation | Syntax |
|---|---|
| Declare an array | int nums[5]; |
| Initialize an array | int nums[] = {1, 2, 3}; |
| Access an element | nums[0] = 10; |
| Array length (stack arrays) | sizeof(nums) / sizeof(nums[0]) |
| String literal | char s[] = "hello"; |
| String length | strlen(s) (excludes \0) |
| Copy a string | strcpy(dst, src); |
| Compare strings | strcmp(a, b) == 0 means equal |
| Concatenate strings | strcat(dst, src); |
Structs & common stdlib
| Operation | Syntax |
|---|---|
| Define a struct | struct Point { int x; int y; }; |
| Declare & access | struct Point p; p.x = 1; |
| Access via pointer | p->x = 1; (same as (*p).x) |
| Type alias | typedef struct Point Point; |
| Read input | scanf("%d", &n); |
| Convert string to int | int n = atoi("42"); |
| Absolute value / power | abs(x) (int), pow(x, y) from <math.h> |
| Sort an array | qsort(arr, n, sizeof(int), cmp); |
The C syntax, format specifiers, and pointer patterns you reach for most, on one page. This C cheat sheet is a quick reference for writing C - the data types, printf/scanf specifiers, operators, control flow, and the pointer and memory operations that trip people up.
Everything here is standard C (C99/C11) and compiles with gcc or clang. Copy what you need, or try any snippet live in the C playground - no compiler to install.
C cheat sheet FAQ
Is this C cheat sheet free?
What is the difference between the stack and the heap in C?
malloc or calloc, lives until you call free, and is used when you need memory that outlives the current function or whose size is known only at runtime. Forgetting to free heap memory causes a memory leak.Why does scanf need an ampersand (&) but printf does not?
scanf writes the value back into your variable, so it needs the variable's address - that is what &x gives it. printf only reads the value, so you pass the value itself. The exception is strings: a char array already decays to a pointer, so you pass scanf("%s", name) without the &.