int
Typically 4 bytes (on 32-bit systems like STM32). The standard “whole number” type.
char
1 byte. Usually holds an ASCII character or a small integer (-128 to 127).
float
4 bytes. Used for decimal numbers. Note: Some small microcontrollers don’t have a “Floating Point Unit” (FPU), making these slow!
double
8 bytes. Double-precision decimal. Even “heavier” than a float.
bool
1 byte. Stores 0 (false) or 1 (true).
Array
A collection of the same data type stored in contiguous (back-to-back) memory.Interview Tip: Accessing an array is $O(1)$ speed because the CPU just calculates an offset from the start.
Struct
A custom container that groups different types together.
Example: A struct Sensor { int id; float value; }; takes up 8 bytes of memory (4 for the int + 4 for the float).
Union
A container where all members share the same starting memory address.
Why use it? To save space. If you have a union with an int and a char, it only takes up 4 bytes (the size of the largest member).
Pointer
A variable that stores the memory address of another variable.
Size: Usually 4 bytes on a 32-bit system or 8 bytes on a 64-bit system.
What happens to the memory address of a pointer p if you perform p++, and how does the data type of the pointer affect this?
The pointer p will point to the next memory address of its specific data type. The jump in address is equal to sizeof(type). For example, incrementing a uint32_t* moves the address forward by 4 bytes, while a char* moves by 1 byte.
In an embedded system, why would you declare a variable pointing to a hardware status register as volatile?
Hardware registers can change their value independently of the program’s execution (e.g., a bit being set by a sensor). Without volatile, the compiler might optimize the code by reading the value into a CPU register once and never checking the actual hardware memory again, causing the program to miss status changes.
If you have a struct containing one char followed by one int, why might sizeof(struct) return 8 bytes instead of 5 bytes?
This is due to memory alignment or padding. Most 32-bit processors (like an STM32) are optimized to read 4-byte integers from addresses that are multiples of 4. The compiler inserts “padding” bytes after the char to ensure the int starts on a 4-byte boundary for faster CPU access.