C for systems programming
Type sizes and LP64, pointers and `a[i]`, struct layout and padding, memory segments, UB, the gcc/clang pipeline and headers.
Types and pointers
C is the language of kernels, libc, embedded firmware and the glue under higher-level runtimes because it maps closely onto the machine the architecture half described: fixed-width integers, addresses as first-class values, and structs as explicit memory layouts. There is no garbage collector and no green-thread scheduler unless you build them — which is why system interfaces are specified in C even when called from Python or Go.
Sizes are ABI facts. LP64 (64-bit Linux and macOS): int 4 bytes, long 8, pointers 8. ILP32 (RV32 and classic 32-bit): int, long and pointers are 4. Windows 64-bit is LLP64 (long stays 4). When a wire format or file format needs an exact width, use <stdint.h>: uint32_t, int64_t, uintptr_t. Mixing int and pointers silently truncates on LP64 and has launched many security bugs.
A pointer is a typed address. The identity a[i] == *(a+i) is definitional, and p+i advances bytes. Byte-oriented APIs take void * and a length; typed APIs take struct foo *. Alignment faults on some ISAs (and slower paths on others) punish misaligned pointers cast from raw buffers — pack wire structs carefully or copy field-by-field.
Structs and process memory
The compiler lays struct members in order, inserting padding so each member’s offset is a multiple of its alignment; the total size is a multiple of the strictest alignment. Swapping field order can shrink a struct from 32 bytes to 16; offsetof and sizeof are the authority, not mental arithmetic. Packed attributes (__attribute__((packed))) remove padding for wire formats at the cost of generated misaligned accesses — use deliberately.
Know the regions of a process: .text, .rodata, .data, .bss, heap upward via malloc/mmap, stack downward for locals and return addresses. Lifetime follows region: locals die at brace exit; malloc lives until free; statics live for the process. Use-after-free and double-free are undefined behaviour with exploit shapes, not mere leaks.
UB and the build
Undefined behaviour is the bargain that lets compilers optimize aggressively: signed overflow, OOB array access, null dereference, dangling pointers, uninitialized reads. If the abstract machine says those never happen, then x+1 > x for signed x is identically true and becomes 1 at -O2. Enable -Wall -Wextra, and run tests under -fsanitize=address,undefined (and thread sanitizer when you reach synchronization).
Build stages: preprocessor (-E) pastes headers and expands macros; compiler (-S) emits assembly; assembler (-c) emits an object; the linker resolves symbols across objects and libraries. Declare in headers, define once. The next topic takes the functions you write — open, read, write — through the trap into the kernel.
This topic’s checks ask for concrete numbers from the prose — page-sized PIPE_BUF and mode 0640 as 416, syscall number 64, microkernel crossings 8, busy fraction 20% — so that the abstractions stay pinned to the machine.
Integer promotions and usual arithmetic conversions still surprise systems programmers: mixing uint32_t with int can yield signed comparisons you did not want; shifting by ≥ width is undefined; right-shifting signed negatives is implementation-defined until C23’s clearer rules. Prefer unsigned types for bit masks and sizes that must not go negative, and size_t for object sizes.
Varargs (printf-style) pass arguments under special rules: float promotes to double, small ints to int, and there is no type checking unless the compiler understands the format string (-Wformat). Writing your own logging with varargs is fine; parsing untrusted format strings is not.
Linkage matters at scale. static at file scope gives internal linkage; extern declarations match a single definition. Inline functions in headers need careful static inline or C99 inline rules to avoid duplicate symbols. The build topic’s driver flags (-I, -L, -l, -fPIC) become daily tools once you link against libc, libpthread and third-party libraries the later networking and runtime topics assume.
Worked habit: for every new abstraction, write down (1) the kernel object, (2) the creating syscall, (3) what is shared after fork, (4) how EOF or termination is signalled, and (5) one concrete number (size, timeout, default limit). That checklist turns this course’s vocabulary into a debugging script you can run under strace.
Tooling closes the loop. gdb or lldb step at the line or instruction level; objdump -d shows what the compiler actually emitted for a struct access; nm and readelf/llvm-readobj show symbols the linker resolved. When -O2 makes a bug “disappear,” that is often undefined behaviour being optimized away — rebuild with sanitizers before blaming the optimizer. The systems half of the course will keep handing you C snippets; treat each as a contract with the ABI and the kernel, not as a scripting language with types.
Re-read the block markers as a study plan: each block’s checks are capped so that finishing the prose and the questions in one sitting stays realistic for Pulse. If a number in a check surprises you, jump back to the sentence that introduced it rather than memorizing in isolation.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.