Bird
Raised Fist0
Embedded Cprogramming~20 mins

Signed vs unsigned behavior on 8-bit MCU in Embedded C - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
8-bit MCU Signed vs Unsigned Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of signed and unsigned addition on 8-bit MCU

What is the output of this code snippet running on an 8-bit microcontroller where char is 8 bits?

Embedded C
#include <stdio.h>

int main() {
    unsigned char a = 250;
    signed char b = 10;
    unsigned char c = a + b;
    printf("%u\n", c);
    return 0;
}
A4
B250
C10
D260
Attempts:
2 left
💡 Hint

Remember that unsigned char wraps around at 255.

❓ Predict Output
intermediate
2:00remaining
Signed overflow behavior on 8-bit MCU

What is the output of this code snippet on an 8-bit MCU where char is signed 8-bit?

Embedded C
#include <stdio.h>

int main() {
    signed char x = 127;
    signed char y = 1;
    signed char z = x + y;
    printf("%d\n", z);
    return 0;
}
A127
B128
C0
D-128
Attempts:
2 left
💡 Hint

Think about signed overflow and two's complement wrap-around.

🔧 Debug
advanced
2:00remaining
Why does this unsigned subtraction produce a large number?

Consider this code on an 8-bit MCU:

#include <stdio.h>

int main() {
    unsigned char a = 5;
    unsigned char b = 10;
    unsigned char c = a - b;
    printf("%u\n", c);
    return 0;
}

Why does it print 251 instead of a negative number?

ABecause the compiler converts unsigned to signed before subtraction
BBecause the printf format specifier is wrong and prints garbage
CBecause unsigned subtraction wraps around modulo 256, so 5 - 10 = 251
DBecause the MCU does not support subtraction on unsigned types
Attempts:
2 left
💡 Hint

Think about how unsigned arithmetic works on small fixed-size types.

❓ Predict Output
advanced
2:00remaining
Result of mixed signed and unsigned comparison

What is the output of this code on an 8-bit MCU?

Embedded C
#include <stdio.h>

int main() {
    signed char a = -1;
    unsigned char b = 255;
    if (a == b) {
        printf("Equal\n");
    } else {
        printf("Not Equal\n");
    }
    return 0;
}
ANot Equal
BEqual
CCompilation error
DUndefined behavior
Attempts:
2 left
💡 Hint

Integer promotions: both char types promote to signed int. -1 != 255.

🧠 Conceptual
expert
3:00remaining
Why is signed integer overflow undefined but unsigned overflow well-defined?

On an 8-bit MCU, why does C define signed integer overflow as undefined behavior, but unsigned integer overflow wraps around modulo 256?

ABecause the compiler automatically converts signed overflow to unsigned overflow internally
BBecause unsigned overflow is defined by the C standard to wrap modulo 2^n, but signed overflow can cause unpredictable hardware states and is left undefined for optimization
CBecause signed integers use two's complement and unsigned integers use sign-magnitude representation
DBecause unsigned integers are stored differently in memory than signed integers
Attempts:
2 left
💡 Hint

Think about the C language standard and hardware behavior.

Practice

(1/5)
1. What is the range of values for an 8-bit signed char on a typical MCU?
easy
A. -128 to 127
B. 0 to 255
C. -256 to 255
D. -127 to 128

Solution

  1. 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.
  2. Step 2: Compare with options

    Only -128 to 127 matches the correct range for signed 8-bit char.
  3. Final Answer:

    -128 to 127 -> Option A
  4. Quick Check:

    Signed 8-bit char range = -128 to 127 [OK]
Hint: Signed 8-bit char always includes negative half [OK]
Common Mistakes:
  • Confusing signed and unsigned ranges
  • Assuming 8-bit signed char goes to 255
  • Mixing up sign bit usage
2. Which of the following declarations correctly defines an unsigned 8-bit variable in embedded C?
easy
A. signed char var;
B. int8_t var;
C. char unsigned var;
D. unsigned char var;

Solution

  1. Step 1: Identify unsigned 8-bit type syntax

    In embedded C, unsigned char is the standard way to declare an unsigned 8-bit variable.
  2. Step 2: Check other options

    signed char var; is signed, char unsigned var; is invalid syntax, int8_t var; is signed 8-bit integer.
  3. Final Answer:

    unsigned char var; -> Option D
  4. Quick Check:

    Unsigned 8-bit variable = unsigned char [OK]
Hint: Use 'unsigned char' for unsigned 8-bit variables [OK]
Common Mistakes:
  • Using 'char unsigned' which is invalid syntax
  • Assuming 'int8_t' is unsigned
  • Confusing signed and unsigned keywords
3. Consider this code snippet on an 8-bit MCU:
unsigned char a = 250;
signed char b = (signed char)a;
printf("%d", b);

What is the output?
medium
A. 250
B. -6
C. 6
D. -250

Solution

  1. Step 1: Understand casting from unsigned to signed

    Variable a holds 250 (unsigned). Casting to signed char wraps around because signed char max is 127.
  2. Step 2: Calculate signed char value

    250 - 256 = -6 (wrap-around in 8-bit signed range)
  3. Final Answer:

    -6 -> Option B
  4. Quick Check:

    Unsigned 250 cast to signed 8-bit = -6 [OK]
Hint: Subtract 256 if unsigned > 127 when casting to signed [OK]
Common Mistakes:
  • Assuming cast keeps original unsigned value
  • Confusing sign extension with wrap-around
  • Ignoring 8-bit overflow behavior
4. This code runs on an 8-bit MCU:
unsigned char x = 200;
signed char y = -100;
unsigned char z = x + y;
printf("%u", z);

What is the output?
medium
A. 156
B. 44
C. 100
D. 300

Solution

  1. Step 1: Understand integer promotion in addition

    In expressions, unsigned char and signed char are promoted to int before addition.
  2. 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.
  3. Step 3: Print the result

    printf with %u prints unsigned int, so output is 100.
  4. Final Answer:

    100 -> Option C
  5. Quick Check:

    Signed promoted to int before addition, result 100 [OK]
Hint: Signed promoted to int before addition, watch final cast [OK]
Common Mistakes:
  • Adding raw unsigned and signed bytes without promotion
  • Assuming overflow without checking promotion
  • Misinterpreting printf format specifiers
5. On an 8-bit MCU, you want to store the result of subtracting 150 from 100 using unsigned 8-bit variables:
unsigned char a = 100;
unsigned char b = 150;
unsigned char c = a - b;
printf("%u", c);

What will be printed and why?
hard
A. 206 because unsigned subtraction wraps around modulo 256
B. -50 because subtraction keeps sign in unsigned
C. 50 because it calculates absolute difference
D. Error due to negative result in unsigned

Solution

  1. Step 1: Understand unsigned subtraction wrap-around

    Unsigned 8-bit subtraction wraps modulo 256. 100 - 150 = -50, which wraps to 256 - 50 = 206.
  2. Step 2: Confirm printf output

    Variable c holds 206, so printf with %u prints 206.
  3. Final Answer:

    206 because unsigned subtraction wraps around modulo 256 -> Option A
  4. Quick Check:

    Unsigned subtraction wraps modulo 256 = 206 [OK]
Hint: Unsigned subtraction wraps modulo 256 on 8-bit MCU [OK]
Common Mistakes:
  • Expecting negative numbers in unsigned arithmetic
  • Assuming error on negative result
  • Thinking subtraction gives absolute difference
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard