1. Structs — layout and size
- Fields in declaration order; compiler may pad to alignment.
struct A { char c; int i; };
printf("%zu\n", sizeof(struct A)); // >= 5, usually 8 (padding)
- Packing:
#pragma pack(1)(or attribute) removes padding — smaller/slower, used for wire formats. - Alignment: struct offset must be multiple of member alignment.
struct A { char c; int i; }; // offset c=0, i=4 once padded
2. Unions
- All members share the same memory; size = largest member.
- Writing one member reads the other — type-punning territory (UB beyond the last written member in C++ strict-aliasing terms; in C it’s the trick).
union U { int i; float f; };
printf("%zu\n", sizeof(union U)); // 4
3. Bit fields
- Fields in bits directly:
struct Flags { unsigned a : 3; unsigned b : 5; };. - Wraps at the declared width; packing depends on layout; portability limited. Use when you need flag compression.
4. Preprocessor & macros
#define NAME value— textual substitution, no type.- Function-like macros run before the compiler; no runtime cost.
#define SQUARE(x) ((x) * (x))
SQUARE(1 + 2) // ((1+2)*(1+2))
- Always parenthesise params and the whole body.
#stringify:#x;##concatenation:
#define TO_STR(x) #x // argument to string
#define CAT(a,b) a##b
- Precedence trap:
2 * SQUARE(3)parenthesised is fine; unparenthesised isn’t. #ifdef,#ifndef,#elif,#endifconditional compilation;#pragma once+include guards.
5. Bitwise operators (interview pack)
| Op | Meaning |
|---|---|
&, |, ^, ~ | and, or, xor, not |
<<, >> | shift left / right |
Patterns: set a bit x |= (1 << n); clear x &= ~(1 << n); flip x ^= (1 << n); test x & (1 << n).
- Extracting bits:
(x >> start) & mask. - Be careful with signed right shift (implementation-defined arithmetic vs logical shift).
6. Operator precedence — the scoreless overview
High → low (memory aide):
- Postfix
()[].->++/— - Unary
+ - ! ~ * & (type) ++/-- * / %+ -<< >>< > <= >=== !=&,^,|- Logical
&&,|| - Ternary
?: - Assignment and
+=etc. - Comma
Interview golden rule: write parentheses whenever precedence is non-obvious — the compiler isn’t your reviewer.
7. Interview checkpoint
- Struct padding & pragma pack.
- Unions sharing memory.
- Macro parameter parens; stringify/paste.
- Bitwise ops for flags.
- Precedence — especially
<<and assignment mixing.
Junior fluency bonus: know that sizeof and & have higher precedence than everything else binary.
Premium Content
Unlock Part 3: Structs, Unions, Preprocessor & Operators and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans