C Programming formula sheet
17 formulas in 3 topics, each with when to use it. Each is explained in the topic notes.
n-bit integer ranges
Holds for any fixed-width integer type once its width in bits and its signedness are known. Use it to check whether a constant fits the type it is stored in.
Storing an out-of-range value v in a signed n-bit type
The standard leaves this conversion implementation-defined. On the two's complement machines these notes assume, the result is the value above. For example, 200 stored in a signed 8-bit char gives . An unsigned -bit type always stores , and that case is defined by the standard.
1-D array indexing / pointer arithmetic
For any array a with element type T. The same value is the address a + i. C arrays always start at index 0, and memory is byte-addressable.
2-D row-major address (C arrays)
The array is declared T a[R][C]. C is the number of COLUMNS (the row length), i is the row index and j is the column index, both starting at 0. The number of rows R does not appear.
Call count: single (linear) recursion
For a function that makes exactly one recursive call, on , and stops at n == 0 without a further call (for example, a factorial with base case n == 0). counts the initial call and the base-case call. If the base case is at n == 1 instead (with ), the count is . Always check where the base case fires.
Call count: halving recursion
For a function that makes one recursive call on n / 2 (integer division) and stops at n == 0, like bin above. The calls that print or do work (those with ) number , which is the number of bits of . The extra call is the base case n == 0.