0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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.