Atlas Robotics
C/C++ ProgrammingBasic

Variables and data types in embedded C/C++

Atlasbot · 16/8/2026 · 10 min read

Variables and data types in embedded C/C++
Size of data types in C

In normal application programming, declaring int or long rarely requires much thought — computers have gigabytes of RAM. But on a microcontroller with only a few KB of RAM, every byte costs, and the actual size of a data type (not just the name) becomes something that must be understood.

Key concepts

A variable in C is a named memory location with a defined data type How many bytes does it take up? and How is it interpreted? (signed integer, unsigned integer, real number...). Problems with basic styles (int, long, char) is their size not fixed — depends on chip architecture and compiler. One int might be 2 bytes on an older 8-bit microcontroller, but 4 bytes on an ARM Cortex-M — same line of code, two different results.

fixed-width types

To avoid this ambiguity, embedded code almost always uses types declared in <stdint.h>, whose size is fixed on all platforms: uint8_t, int8_t, uint16_t, int16_t, uint32_t... — the type name directly states the number of bits it is account, no need to guess.

In short: Use uint8_t, int16_t... instead of int not to "look like sample code" — but because it ensures you know exactly how many bytes the variable is taking up, important when RAM is only 2–512KB and when mapping directly to hardware registers (which have a fixed width 8/16/32-bit).

How it works

When declaring uint8_t counter = 0;, the compiler allocates exactly 1 byte in RAM, and any operations on counter interpret those 8 bits according to the unsigned rule (values 0-255). With the signed type (int8_t), the same 8 bits are interpreted as 2's complement (two's complement) — the highest bit has the sign meaning, so the range drops to -128 to 127 even though there are still only 8 physical bits.

uint8_t (unsigned): 0000 0000 → 0 1111 1111 → 255
int8_t (signed): 0000 0000 → 0 1111 1111 → -1 (2's complement)

This is why the same bit string can be two completely different values ​​depending on the declaration type — it's not that the hardware changes the way it's stored, it's the compiler that changes the way interprets.

Technical details

Formulas

The range of representable values of an N-bit type is calculated directly from N:

Unsigned: 0 to (2^N − 1)
Signed (signed, 2's complement): −2^(N−1) to (2^(N−1) − 1)

VD: N = 8
Unsigned: 0 to 255
Signed: −128 to 127

When the value exceeds this range, the result is "wrap-around" rather than a compilation error:

uint8_t counter = 250;
counter += 10;   // (250 + 10) mod 256 = 4, not 260

Parameters

Sizes and value ranges of common C data types in embedded programming:

TypeSizeValue range
uint8_t1 byte0 – 255
int8_t1 byte-128 – 127
uint16_t2 byte0 – 65.535
int16_t2 byte-32.768 – 32.767
uint32_t4 byte0 – 4.294.967.295
float4 byte~7 significant decimal places

Register / Bit / Byte

When directly accessing a hardware register by address (not through the HAL library), the variable must declare the correct width of the actual register and adding the keyword volatile — telling the compiler that this value can be changed automatically by the hardware, not optimized to skip re-reading:

// Access a 32-bit register at a specific address on the STM32
volatile uint32_t *gpioa_moder = (uint32_t *)0x40020000;
*gpioa_moder |= (1 << 10);   // set bit 10, leave other bits unchanged

Missing volatile here is a classic error: the compiler can "remember" the value read the first time and not read it back to the hardware the next time, causing the code to always see the old value even though the actual register has changed.

Practical applications

When reading raw data from the IMU sensor via I2C (Example: MPU6050), the acceleration of each axis returned is 2 bytes, signed (int16_t) — wrongly splicing the byte order or using the wrong unsigned type (uint16_t) causes the negative value (when the sensor is tilted backwards) to be read as a very large positive number instead of a small negative number, completely distorting the calculation tilt angle. Similarly, when defining the data transmission frame structure between two boards (eg: packets sent via UART between the motor control MCU and the safety MCU), using the correct fixed width type (uint8_t, uint16_t) ensures that both sides understand the correct number of bytes of each data field, avoiding frame misalignment when the 2 boards use different compilers/architectures.

Practical example

#include <stdio.h>
#include <stdint.h>

int main() {
    uint8_t counter = 250;
    counter += 10;
    printf("counter = %d\n", counter);   // counter = 4 (number overflow)

    int16_t adc_raw = 700;
    uint32_t scaled_wrong = adc_raw * 1000;          // can overflow before assignment
    uint32_t scaled_safe  = (uint32_t)adc_raw * 1000; // press before multiplying
    printf("scaled_safe = %u\n", scaled_safe);        // scaled_safe = 700000
    return 0;
}
Output:
counter = 4
scaled_safe = 700000

counter = 4 correctly illustrates the overflow formula in the Technical Knowledge section — 260 mod 256 = 4. Casting ((uint32_t)adc_raw) before multiplication is a mandatory step for multiplication to take place at 32-bit width, avoiding overflow in intermediate results before being assigned to a 32-bit variable.

Common mistakes

  • Choosing too narrow a type for the loop counter variable — using uint8_t for the counter variable to run up to 300 times causes the loop to never stop properly (because the variable overflows to 0 before reaching 300) — the code still compiles normally, just wrong when actually run.
  • Forget to cast before multiplying — multiplication between 2 numbers int16_t/uint16_t can overflow even in the intermediate step, before even assigning to the wider result variable — always cast the first operand to wide before multiplying.
  • Forget volatile when declaring a pointer to a hardware register — the compiler can optimize and skip re-reading the actual value, causing the code to always see the value read the first time even if the hardware has changed.
  • Confusion with signs when concatenating bytes into 16/32-bit numbers — concatenating the wrong byte order (endianness) or mistakenly using an unsigned type for signed data (eg, IMU data) causing negative values to be read as very large positive numbers.

Conclusion

The data type in embedded C is not a syntactic detail that can be glossed over — it directly determines how much RAM is occupied, the maximum/minimum values ​​that can be saved, and how to properly interpret raw data from sensors/communications. Always prefer fixed-width styles in <stdint.h> instead of ambiguous int/long. The next article in this section is about Pointer — a tool associated with directly manipulating memory areas and registers mentioned above.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

Variables and data types in embedded C/C++ — Atlas Robotics