Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Data Types Examples in C: sizeof, printf, Overflow and Type Choice
Twelve practical examples with verified output: sizes on ARM versus PC, ranges, printf formatting, unsigned and signed overflow, char signedness, division, float error and SRAM budgeting.
Data Types Examples: What This Page Covers
This page is the hands-on companion to the theory. Each of these data types examples is a complete program that was compiled with gcc -Wall -Wextra and run, so the outputs shown are real. Where the ARM Cortex-M4 result differs from the PC result, the difference is explained.
| # | Example | Skill |
|---|---|---|
| 1 | sizeof of every type: ARM versus PC | Portability |
| 2 | Ranges from limits.h | Min / max |
| 3 | printf with every specifier, width and precision | Formatting |
| 4 | Unsigned overflow: the wrap-around demo | Counters |
| 5 | Signed overflow, safely detected | Undefined behaviour |
| 6 | Is plain char signed on your compiler? | Signedness |
| 7 | Integer versus floating-point division | Fractions |
| 8 | Accumulated float error | Precision |
| 9 | Choosing types to save SRAM | Memory budget |
| 10 | Temperature as scaled integer | Avoiding floats |
| 11 | Type comparison table | Reference |
| 12 | Mini exercises | Self-test |
Example 1: sizeof of Every Type, ARM Cortex-M4 versus a 64-bit PC
Among all data types examples, this is the most important one to run on your own machine. Problem: How many bytes does each type occupy on the STM32F407, and how does that differ from the PC you are developing on?
| Type | ARM Cortex-M4 (arm-none-eabi-gcc) | 64-bit Linux PC | 64-bit Windows | Differs? |
|---|---|---|---|---|
| char | 1 | 1 | 1 | No |
| short | 2 | 2 | 2 | No |
| int | 4 | 4 | 4 | No |
| long | 4 | 8 | 4 | Linux only |
| long long | 8 | 8 | 8 | No |
| float | 4 | 4 | 4 | No |
| double | 8 | 8 | 8 | No |
| long double | 8 | 16 | 16 (MSVC: 8) | Yes |
| void * | 4 | 8 | 8 | Yes |
| size_t | 4 | 8 | 8 | Yes |
long on LP64, pointers, size_t) change size when you move from a 32-bit MCU to a 64-bit PC. A host-side unit test of firmware code must not assume ARM sizes.Example 2: Reading the Ranges from limits.h
Problem: Print the minimum and maximum of each integer type without typing the numbers by hand.
On ARM the long row would read −2,147,483,648 to 2,147,483,647, identical to int. The last line shows the rule you can use for any unsigned type of N bits: the maximum is 2N − 1, and the signed range is −2N−1 to 2N−1 − 1.
| Bits | Unsigned maximum | Signed minimum | Signed maximum |
|---|---|---|---|
| 8 | 255 | −128 | 127 |
| 16 | 65,535 | −32,768 | 32,767 |
| 32 | 4,294,967,295 | −2,147,483,648 | 2,147,483,647 |
| 64 | 18,446,744,073,709,551,615 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
Example 3: printf Width, Precision and Flags
Problem: Format values neatly for a UART console. The specifier can carry flags (-, 0, +, #), a minimum width and a precision.
%08.3f%08lX (or %08" PRIX32 ") to print a 32-bit register as exactly eight hex digits, for example 0x40020C00 instead of 40020c00 or 20c00.Example 4: Unsigned Overflow Wraps Around
This is the most common of the data types examples in real firmware. Problem: What happens when an unsigned counter goes past its maximum? In C this is fully defined: the value wraps modulo 2N.
(uint32_t)(now - start) >= timeout, which stays correct across the wrap.Example 5: Signed Overflow, Detected Safely
Problem: Unlike unsigned arithmetic, signed overflow is undefined behaviour in C. Check before the operation, or let the compiler do it.
a + b < a? Because the compiler is allowed to assume signed overflow never happens and may delete that test. __builtin_add_overflow (GCC/Clang) or widening to a larger type are safe.Example 6: Is Plain char Signed on Your Compiler?
Among the data types examples on portability, this one catches the most bugs. Problem: Find out how plain char behaves and write code that gives the same answer everywhere.
| Compiler / target | CHAR_MIN | c = (char)0xE9 | c > 100 |
|---|---|---|---|
| x86 / x86-64 GCC | −128 | −23 | no |
| ARM (arm-none-eabi-gcc) | 0 | 233 | yes |
ARM with -fsigned-char | −128 | −23 | no |
uint8_t for bytes (always unsigned) or write unsigned char explicitly. GCC has -funsigned-char / -fsigned-char to force one behaviour project-wide.Example 7: Integer Division versus Floating-Point Division
Problem: Why does 7 / 2 give 3, and how do you get 3.5?
-7 / 2 == -3 and -7 % 2 == -1.Example 8: Accumulated Float Error
Problem: A float has only 24 bits of precision. Adding a small number many times shows the effect.
The float total drifts away from 100000 because each addition rounds, and above 224 = 16,777,216 a float cannot even represent consecutive integers. For counters always use integer types; for sums over long periods use double (on a PC) or Kahan summation, or accumulate in a 32/64-bit integer of scaled units.
Example 9: Choosing Types to Save SRAM
Problem: You buffer 1000 ADC readings (12-bit, range 0..4095). Which element type uses the least SRAM, and what does it cost on the STM32F407 (192 KB total)?
uint16_t is the right choice: uint8_t cannot hold 4095, and int wastes half the memory. With 100 such buffers the difference is 200 KB versus 400 KB, which would not even fit in the STM32F407.Example 10: Temperature as a Scaled Integer (No Floating Point)
Problem: Convert a 12-bit ADC reading from an LM35 sensor to degrees Celsius with one decimal place, without using float.
- The sensor value is stored in
int16_ttenths of a degree, so negative temperatures are possible. - The multiplication uses
int32_t:4095 * 3300is 13.5 million and would overflow a 16-bit type. - Printing uses integer division and remainder, which also works in newlib-nano without float support.
Example 11: Complete Type Comparison Table (ARM Cortex-M4)
Keep this summary of the data types examples next to your keyboard. All values are for arm-none-eabi-gcc on the STM32F407 (plain char is unsigned there).
| Type | Bytes | Minimum | Maximum | printf specifier |
|---|---|---|---|---|
| char / signed char | 1 | −128 | 127 | %hhd |
| unsigned char | 1 | 0 | 255 | %hhu |
| short | 2 | −32,768 | 32,767 | %hd |
| unsigned short | 2 | 0 | 65,535 | %hu |
| int / long (ARM) | 4 | −2,147,483,648 | 2,147,483,647 | %ld |
| unsigned int / long (ARM) | 4 | 0 | 4,294,967,295 | %lu |
| long long | 8 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | %lld |
| unsigned long long | 8 | 0 | 18,446,744,073,709,551,615 | %llu |
| float | 4 | ±1.2e−38 | ±3.4e38 | %f / %e / %g |
| double | 8 | ±2.2e−308 | ±1.8e308 | %f / %e / %g (%lf in scanf) |
Example 12: Mini Exercises
Try each of these data types examples before opening the answer; they revisit sizes, ranges, casting and overflow.
Exercise 1: What is sizeof(int[10]) on the STM32F407?
Exercise 2: Which type would you use for the value of a 16-bit timer counter register (TIM2 on the F407 is 32-bit, TIM3 is 16-bit)?
uint16_t for TIM3’s CNT, uint32_t for TIM2’s. The register width decides the type.Exercise 3: What does uint8_t x = 300; store?
-Woverflow.Exercise 4: Which specifier prints a size_t and which prints a long long?
%zu and %lld.Exercise 5: Will float f = 1 / 4; store 0.25?
1 / 4 is integer division = 0, so f = 0.0. Use 1.0f / 4.Exercise 6: How many values can a 12-bit ADC result take, and which smallest standard type holds them?
unsigned short (16 bit). unsigned char is too small.Exercise 7: A long holds 3,000,000,000 on a 64-bit PC. What happens on the STM32F407?
long maximum is 2,147,483,647, so the value overflows. Use uint32_t or int64_t.Key Takeaways from the Data Types Examples
- In these data types examples,
sizeofresults differ between ARM and PC forlong, pointers andsize_t. - Unsigned overflow wraps (defined); signed overflow is undefined behaviour.
- Plain
charis unsigned on ARM and signed on x86: useuint8_tfor bytes. - Cast an operand before dividing to get floating-point division.
- Pick the smallest type that always fits (
uint16_tfor a 12-bit ADC) to conserve SRAM. - Scaled integers avoid floating point entirely.
Ready for the quiz?
Next: multiple choice, fill-in-the-blank, true/false, code snippet challenges and interview questions on standard C data types.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments