C

1. The Four Primary Modifiers

C provides four modifiers that can be applied to basic data types:

  • signed: Allows the variable to hold both positive and negative values (this is the default for int and char).

  • unsigned: Forces the variable to hold only positive values (and zero). This effectively doubles the maximum positive range.

  • short: Reduces the storage space of a variable (usually to 2 bytes for an integer), which is useful for saving memory in embedded systems.

  • long: Increases the storage space of a variable (usually to 8 bytes for long long), allowing it to store much larger numbers.

2. Common Combinations and Ranges

The size of these types can vary depending on the compiler, but here are the typical values for a 32-bit/64-bit system:

Type CombinationSize (Bytes)Range
short int2$-32,768$ to $32,767$
unsigned short int2$0$ to $65,535$
unsigned int4$0$ to $4,294,967,295$
long long int8Very large (19+ digits)
unsigned char1$0$ to $255$

3. Why Use Modifiers?

As you progress toward STM32 development, you will find that modifiers are critical for two reasons:

  1. Memory Optimization: On microcontrollers with limited RAM, using short instead of int when a value will never exceed 30,000 can save significant space.

  2. Hardware Interaction: When reading sensor data that only yields positive values (like a pulse rate or distance), using unsigned ensures your logic doesn't accidentally treat a large positive value as a negative one.

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now