What if a tiny mistake in number type could crash your whole device without warning?
Signed vs unsigned behavior on 8-bit MCU in Embedded C - When to Use Which
Imagine you are programming a small 8-bit microcontroller to read sensor values and control a device. You try to store numbers in a variable but sometimes get strange results when the values go below zero or above 255.
Using only one type of number without understanding signed or unsigned causes bugs. Negative numbers might wrap around to large positive values, or calculations give wrong results. Debugging these errors by guessing is slow and frustrating.
Knowing the difference between signed and unsigned numbers helps you pick the right variable type. This avoids unexpected wrap-around and makes your program behave correctly and predictably on the 8-bit MCU.
uint8_t x = 250; x = x + 10; // x becomes 4 (wraps around)
int8_t x = 110; x = x + 10; // x becomes 120 (correct signed addition)
Understanding signed vs unsigned lets you write reliable code that handles all number ranges correctly on limited 8-bit hardware.
When reading temperature sensors that can go below zero, using signed variables prevents wrong readings and keeps your device safe and accurate.
Signed and unsigned types store numbers differently on 8-bit MCUs.
Choosing the right type prevents bugs from number wrap-around.
This knowledge makes embedded programs more reliable and easier to debug.
Practice
char on a typical MCU?Solution
Step 1: Understand 8-bit signed char range
An 8-bit signed char uses one bit for sign and 7 bits for value, so it can represent values from -128 to 127.Step 2: Compare with options
Only -128 to 127 matches the correct range for signed 8-bit char.Final Answer:
-128 to 127 -> Option AQuick Check:
Signed 8-bit char range = -128 to 127 [OK]
- Confusing signed and unsigned ranges
- Assuming 8-bit signed char goes to 255
- Mixing up sign bit usage
Solution
Step 1: Identify unsigned 8-bit type syntax
In embedded C,unsigned charis the standard way to declare an unsigned 8-bit variable.Step 2: Check other options
signed char var; is signed, char unsigned var; is invalid syntax, int8_t var; is signed 8-bit integer.Final Answer:
unsigned char var; -> Option DQuick Check:
Unsigned 8-bit variable = unsigned char [OK]
- Using 'char unsigned' which is invalid syntax
- Assuming 'int8_t' is unsigned
- Confusing signed and unsigned keywords
unsigned char a = 250;
signed char b = (signed char)a;
printf("%d", b);What is the output?
Solution
Step 1: Understand casting from unsigned to signed
Variableaholds 250 (unsigned). Casting to signed char wraps around because signed char max is 127.Step 2: Calculate signed char value
250 - 256 = -6 (wrap-around in 8-bit signed range)Final Answer:
-6 -> Option BQuick Check:
Unsigned 250 cast to signed 8-bit = -6 [OK]
- Assuming cast keeps original unsigned value
- Confusing sign extension with wrap-around
- Ignoring 8-bit overflow behavior
unsigned char x = 200;
signed char y = -100;
unsigned char z = x + y;
printf("%u", z);What is the output?
Solution
Step 1: Understand integer promotion in addition
In expressions,unsigned charandsigned charare promoted to int before addition.Step 2: Calculate the sum
200 + (-100) = 100 as int, but when assigned to unsigned char (8-bit), it wraps modulo 256. Since 100 fits in 8 bits, the value is 100.Step 3: Print the result
printf with %u prints unsigned int, so output is 100.Final Answer:
100 -> Option CQuick Check:
Signed promoted to int before addition, result 100 [OK]
- Adding raw unsigned and signed bytes without promotion
- Assuming overflow without checking promotion
- Misinterpreting printf format specifiers
unsigned char a = 100;
unsigned char b = 150;
unsigned char c = a - b;
printf("%u", c);What will be printed and why?
Solution
Step 1: Understand unsigned subtraction wrap-around
Unsigned 8-bit subtraction wraps modulo 256. 100 - 150 = -50, which wraps to 256 - 50 = 206.Step 2: Confirm printf output
Variablecholds 206, so printf with %u prints 206.Final Answer:
206 because unsigned subtraction wraps around modulo 256 -> Option AQuick Check:
Unsigned subtraction wraps modulo 256 = 206 [OK]
- Expecting negative numbers in unsigned arithmetic
- Assuming error on negative result
- Thinking subtraction gives absolute difference
Ready to prove it?
Pass the test and this pattern moves unknown
