Formula sheet

Every formula from every topic. Type to filter.

🔢 Types, Operators & Expressions

Signed n-bit range
−2^(n−1) … 2^(n−1) − 1 (int: −2147483648 … 2147483647)
Unsigned n-bit range
0 … 2^n − 1 (unsigned int max 4294967295)
Unsigned wrap
result = value mod 2^n (so (unsigned)−1 = 2^32 − 1)
Shifts
x << k = x · 2^k x >> k = ⌊x / 2^k⌋
Clear lowest set bit
x & (x − 1) isolate it: x & (−x)
Division and remainder
a == (a / b) * b + a % b, with / truncating toward 0
Masks
n low ones = (1 << n) − 1

🔀 Control Flow & Output

Iterations of for(i=a; i<b; i+=s)
⌈(b − a) / s⌉ (when b > a, s > 0)
Halving loop for(i=n; i>1; i/=2)
⌊log₂ n⌋ iterations
Doubling loop for(j=1; j<n; j*=2)
⌈log₂ n⌉ iterations
Nested loops, inner depends on outer
sum the inner counts: Σ over i of (inner count)
Triple loop i ≤ j < k ≤ n
C(n+1, 3) = (n−1)·n·(n+1) / 6
Switch fall-through
output = every statement from the matched label to the first break

👉 Pointers & Arrays

Address of a[i]
base + i · sizeof(a[0])
Address of a[i][j] in int a[R][C]
base + (i·C + j) · sizeof(int)
Pointer difference
(p − q) = (address(p) − address(q)) / sizeof(*p)
Array and pointer equivalences
a[i] ≡ *(a + i) &a[i] ≡ a + i
sizeof
sizeof(int a[10]) = 40 bytes, but sizeof of an int* parameter = 8
2-D access
a[i][j] ≡ *(*(a + i) + j) ≡ *(a[i] + j)

🔤 Strings & Characters

Storage for a string of n characters
n + 1 bytes (terminator)
sizeof vs strlen
sizeof("abc") = 4, strlen("abc") = 3
ASCII anchors
'0' = 48, 'A' = 65, 'a' = 97, so 'a' − 'A' = 32
Digit to value
c − '0'
strlen(a) − strlen(b)
unsigned: wraps modulo 2^64 when negative

🔁 Functions & Recursion

Depth of linear recursion f(n−1)
n + 1 frames (including the base case)
Calls for f(n) = f(n−1) + f(n−2)
C(n) = C(n−1) + C(n−2) + 1, C(0) = C(1) = 1
Binary recursion halving n
calls = O(log n) with one branch, O(n) with two
Stack space of recursion
depth × size of one frame
Horner loop total = x·total + a[i]
evaluates the polynomial; with x = 2 and all a[i] = 1 gives 2^n − 1

🧠 Storage Classes, Scope & Memory

static local
initialised once; lifetime = whole program; scope = its block
Default values
static and global: 0; auto: indeterminate (garbage)
Where does it live?
local → stack; malloc → heap; global/static → data or BSS; code → text
Frame contents
parameters + return address + saved registers + locals
Leak vs dangling
leak = lost pointer, block never freed; dangling = pointer to freed or dead memory

🧱 Structs, Unions & Undefined Behaviour

Struct size
sum of members + padding, rounded up to a multiple of the largest member alignment
Offset of a member
round the running offset up to the member's alignment
Union size
size of the largest member (rounded up to the largest alignment)
p->x
(*p).x
Array of structs
element i is at base + i · sizeof(struct)