Data Types Examples in C: sizeof, printf, Overflow and Type Choice-Free Embedded C Course Online

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 3 · LESSON 3.1 · EXAMPLES

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.

12
Worked Examples
Beginner
Level
Real output
Compiled and run

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.

#ExampleSkill
1sizeof of every type: ARM versus PCPortability
2Ranges from limits.hMin / max
3printf with every specifier, width and precisionFormatting
4Unsigned overflow: the wrap-around demoCounters
5Signed overflow, safely detectedUndefined behaviour
6Is plain char signed on your compiler?Signedness
7Integer versus floating-point divisionFractions
8Accumulated float errorPrecision
9Choosing types to save SRAMMemory budget
10Temperature as scaled integerAvoiding floats
11Type comparison tableReference
12Mini exercisesSelf-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?

sizes.c
#include <stdio.h> int main(void) { printf(“char : %zu\n”, sizeof(char)); printf(“short : %zu\n”, sizeof(short)); printf(“int : %zu\n”, sizeof(int)); printf(“long : %zu\n”, sizeof(long)); printf(“long long : %zu\n”, sizeof(long long)); printf(“float : %zu\n”, sizeof(float)); printf(“double : %zu\n”, sizeof(double)); printf(“long double : %zu\n”, sizeof(long double)); printf(“void * : %zu\n”, sizeof(void *)); printf(“size_t : %zu\n”, sizeof(size_t)); return 0; }
Output on a 64-bit Linux PC (LP64)
char : 1 short : 2 int : 4 long : 8 long long : 8 float : 4 double : 8 long double : 16 void * : 8 size_t : 8
TypeARM Cortex-M4 (arm-none-eabi-gcc)64-bit Linux PC64-bit WindowsDiffers?
char111No
short222No
int444No
long484Linux only
long long888No
float444No
double888No
long double81616 (MSVC: 8)Yes
void *488Yes
size_t488Yes
Pointer and long: the two sizes that change most
ARM void *
B0
B1
B2
B3
32-bit address: reaches 4 GB (0x00000000 – 0xFFFFFFFF)
PC void *
B0
B1
B2
B3
B4
B5
B6
B7
64-bit address
ARM long
B0
B1
B2
B3
Linux long
B0
B1
B2
B3
B4
B5
B6
B7
Takeaway: The types that match a CPU register (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.

ranges.c
#include <stdio.h> #include <limits.h> int main(void) { printf(“%-18s %12d %12d\n”, “signed char”, SCHAR_MIN, SCHAR_MAX); printf(“%-18s %12d %12d\n”, “short”, SHRT_MIN, SHRT_MAX); printf(“%-18s %12d %12d\n”, “int”, INT_MIN, INT_MAX); printf(“%-18s %12ld %12ld\n”, “long (PC)”, LONG_MIN, LONG_MAX); printf(“%-18s %12d %12u\n”, “unsigned char”, 0, (unsigned)UCHAR_MAX); printf(“%-18s %12d %12u\n”, “unsigned short”, 0, (unsigned)USHRT_MAX); printf(“%-18s %12d %12u\n”, “unsigned int”, 0, UINT_MAX); printf(“\n2^N – 1 check: (1u << 16) – 1 = %u\n”, (1u << 16) – 1); return 0; }
Output (verified by compiling and running)
signed char -128 127 short -32768 32767 int -2147483648 2147483647 long (PC) -9223372036854775808 9223372036854775807 unsigned char 0 255 unsigned short 0 65535 unsigned int 0 4294967295 2^N – 1 check: (1u << 16) – 1 = 65535

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.

BitsUnsigned maximumSigned minimumSigned maximum
8255−128127
1665,535−32,76832,767
324,294,967,295−2,147,483,6482,147,483,647
6418,446,744,073,709,551,615−9,223,372,036,854,775,8089,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.

format_demo.c
#include <stdio.h> int main(void) { int n = 255; float f = 3.14159265f; double d = 1234.5678; unsigned u = 48879; printf(“[%d] [%5d] [%-5d] [%05d] [%+d]\n”, n, n, n, n, n); printf(“[%x] [%X] [%#x] [%08X] [%o] [%#o]\n”, n, n, n, n, n, n); printf(“[%u] [%c] [%s] [%10s] [%-10s]\n”, u, ‘Z’, “ARM”, “ARM”, “ARM”); printf(“[%f] [%.2f] [%8.3f] [%e] [%g]\n”, f, f, f, d, d); printf(“[%.0f] [%5.1f] [%-8.2f]\n”, 2.5, d, f); printf(“%d%% of %d is %d\n”, 25, 200, 200 * 25 / 100); return 0; }
Output (verified by compiling and running)
[255] [ 255] [255 ] [00255] [+255] [ff] [FF] [0xff] [000000FF] [377] [0377] [48879] [Z] [ARM] [ ARM] [ARM ] [3.141593] [3.14] [ 3.142] [1.234568e+03] [1234.57] [2] [1234.6] [3.14 ] 25% of 200 is 50
Anatomy of a format specifier: %08.3f
%
start
0
flag: pad with zeros
8
width: 8 chars
.3
precision: 3 decimals
f
conversion: double
Register dumps: Use %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.

unsigned_wrap.c
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t counter = 250; printf(“uint8_t counter counting up from 250:\n”); for (int i = 0; i < 9; i++) { printf(“%u “, counter); counter++; /* wraps from 255 to 0 */ } printf(“\n”); unsigned int big = 4294967295u; printf(“UINT_MAX = %u\n”, big); printf(“UINT_MAX + 1 = %u\n”, big + 1u); printf(“0 – 1 = %u\n”, 0u – 1u); double days = 4294967296.0 / 1000.0 / 60.0 / 60.0 / 24.0; printf(“A 32-bit millisecond tick wraps after %.1f days\n”, days); return 0; }
Output (verified by compiling and running)
uint8_t counter counting up from 250: 250 251 252 253 254 255 0 1 2 UINT_MAX = 4294967295 UINT_MAX + 1 = 0 0 – 1 = 4294967295 A 32-bit millisecond tick wraps after 49.7 days
uint8_t wraps from 255 back to 0
250
251
252
253
254
255
0
1
2
255 + 1 = 256 does not fit in 8 bits: the carry is lost and 0 remains
Real bug: A 16-bit millisecond timestamp wraps every 65.5 s. A 32-bit one wraps after 49.7 days. Write time comparisons as (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.

signed_overflow.c
#include <stdio.h> #include <stdint.h> #include <limits.h> int main(void) { int a = INT_MAX, b = 1, result; if (__builtin_add_overflow(a, b, &result)) printf(“INT_MAX + 1 would overflow, caught safely\n”); int8_t x = 127; int wide = x + 1; /* computed in int: no overflow */ int8_t narrowed = (int8_t)wide; /* implementation-defined conversion */ printf(“127 + 1 as int = %d\n”, wide); printf(“same stored in int8_t = %d\n”, narrowed); unsigned u = (unsigned)INT_MAX + 1u; /* well-defined */ printf(“(unsigned)INT_MAX + 1 = %u = 0x%X\n”, u, u); return 0; }
Output (verified by compiling and running)
INT_MAX + 1 would overflow, caught safely 127 + 1 as int = 128 same stored in int8_t = -128 (unsigned)INT_MAX + 1 = 2147483648 = 0x80000000
Why not just test 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.

char_sign.c
#include <stdio.h> #include <limits.h> int main(void) { char c = (char)0xE9; /* a byte with the top bit set */ printf(“CHAR_MIN = %d -> plain char is %s\n”, CHAR_MIN, CHAR_MIN < 0 ? “SIGNED” : “UNSIGNED”); printf(“c = %d\n”, c); printf(“c > 100 ? %s\n”, c > 100 ? “yes” : “no”); printf(“(unsigned char)c = %u\n”, (unsigned char)c); return 0; }
Output on x86 GCC (on arm-none-eabi-gcc: UNSIGNED, c = 233, c &gt; 100 yes)
CHAR_MIN = -128 -> plain char is SIGNED c = -23 c > 100 ? no (unsigned char)c = 233
Compiler / targetCHAR_MINc = (char)0xE9c > 100
x86 / x86-64 GCC−128−23no
ARM (arm-none-eabi-gcc)0233yes
ARM with -fsigned-char−128−23no
Portable fix: Use 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?

division.c
#include <stdio.h> int main(void) { int sum = 7, count = 2; printf(“sum / count = %d\n”, sum / count); printf(“sum %% count = %d\n”, sum % count); printf(“(float)sum / count = %.2f\n”, (float)sum / count); printf(“(float)(sum / count) = %.2f\n”, (float)(sum / count)); printf(“sum / 2.0f = %.2f\n”, sum / 2.0f); printf(“-7 / 2 = %d, -7 %% 2 = %d\n”, –7 / 2, –7 % 2); return 0; }
Output (verified by compiling and running)
sum / count = 3 sum % count = 1 (float)sum / count = 3.50 (float)(sum / count) = 3.00 sum / 2.0f = 3.50 -7 / 2 = -3, -7 % 2 = -1
Where the cast goes matters
(float)(sum / count)
7 / 2 → 3 (int division) → 3.00
Wrong: fraction already lost
(float)sum / count
7.0f / 2 → 3.5 (float division)
Right: cast the operand first
Remember: Integer division truncates toward zero (C99), so -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.

float_error.c
#include <stdio.h> int main(void) { float sf = 0.0f; double sd = 0.0; for (int i = 0; i < 1000000; i++) { sf += 0.1f; sd += 0.1; } printf(“float sum of 1,000,000 x 0.1 = %f\n”, sf); printf(“double sum of 1,000,000 x 0.1 = %f\n”, sd); printf(“exact answer = 100000.000000\n”); float big = 16777216.0f; /* 2^24 */ printf(“2^24 + 1 in float = %.1f\n”, big + 1.0f); return 0; }
Output (verified by compiling and running)
float sum of 1,000,000 x 0.1 = 100958.343750 double sum of 1,000,000 x 0.1 = 100000.000001 exact answer = 100000.000000 2^24 + 1 in float = 16777216.0

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)?

sram_budget.c
#include <stdio.h> #include <stdint.h> #define SAMPLES 1000 int main(void) { int a_int[SAMPLES]; uint16_t a_u16[SAMPLES]; uint8_t a_u8[SAMPLES]; printf(“int array: %zu bytes\n”, sizeof(a_int)); printf(“uint16_t array: %zu bytes\n”, sizeof(a_u16)); printf(“uint8_t array: %zu bytes\n”, sizeof(a_u8)); printf(“elements in int array: %zu\n”, sizeof(a_int) / sizeof(a_int[0])); return 0; }
Output (verified by compiling and running)
int array: 4000 bytes uint16_t array: 2000 bytes uint8_t array: 1000 bytes elements in int array: 1000
SRAM used by 1000 samples (bar = share of 192 KB)
int (4 B)
4,000 bytes — 2.0% of SRAM
uint16_t (2 B)
2,000 bytes — 1.0% of SRAM
uint8_t (1 B)
1,000 bytes — 0.5% of SRAM
Decision: 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.

scaled_temp.c
#include <stdio.h> #include <stdint.h> /* Store temperature in tenths of a degree: 23.5 C is held as 235 */ static int16_t celsius_x10_from_adc(uint16_t adc) { /* LM35: 10 mV per degree C, 3.3 V reference, 12-bit ADC */ int32_t mv = (int32_t)adc * 3300 / 4095; return (int16_t)mv; /* mV equals tenths of a degree */ } int main(void) { uint16_t samples[4] = { 0, 290, 1000, 4095 }; for (int i = 0; i < 4; i++) { int16_t t = celsius_x10_from_adc(samples[i]); printf(“adc=%4u -> %d.%d C\n”, samples[i], t / 10, t % 10); } return 0; }
Output (verified by compiling and running)
adc= 0 -> 0.0 C adc= 290 -> 23.3 C adc=1000 -> 80.5 C adc=4095 -> 330.0 C
  • The sensor value is stored in int16_t tenths of a degree, so negative temperatures are possible.
  • The multiplication uses int32_t: 4095 * 3300 is 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).

TypeBytesMinimumMaximumprintf specifier
char / signed char1−128127%hhd
unsigned char10255%hhu
short2−32,76832,767%hd
unsigned short2065,535%hu
int / long (ARM)4−2,147,483,6482,147,483,647%ld
unsigned int / long (ARM)404,294,967,295%lu
long long8−9,223,372,036,854,775,8089,223,372,036,854,775,807%lld
unsigned long long8018,446,744,073,709,551,615%llu
float4±1.2e−38±3.4e38%f / %e / %g
double8±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?
10 × 4 = 40 bytes.
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?
300 mod 256 = 44 (0x2C). Compilers warn with -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?
No. 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?
212 = 4096 values; 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?
A signed 32-bit 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, sizeof results differ between ARM and PC for long, pointers and size_t.
  • Unsigned overflow wraps (defined); signed overflow is undefined behaviour.
  • Plain char is unsigned on ARM and signed on x86: use uint8_t for bytes.
  • Cast an operand before dividing to get floating-point division.
  • Pick the smallest type that always fits (uint16_t for 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

Leave a Reply

Your email address will not be published. Required fields are marked *