Challenge - 5 Problems
Fixed-width Integer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of uint8_t overflow
What is the output of this C code snippet when using
uint8_t for variable a?Embedded C
#include <stdio.h> #include <stdint.h> int main() { uint8_t a = 250; a += 10; printf("%u\n", a); return 0; }
Attempts:
2 left
💡 Hint
Remember that uint8_t can only hold values from 0 to 255 and wraps around on overflow.
✗ Incorrect
The variable 'a' is an 8-bit unsigned integer. Adding 10 to 250 results in 260, but since it can only hold values up to 255, it wraps around modulo 256. So, 260 - 256 = 4 is printed.
❓ Predict Output
intermediate2:00remaining
Size of fixed-width integers
What is the output of this code printing the sizes of
uint8_t, uint16_t, and uint32_t in bytes?Embedded C
#include <stdio.h> #include <stdint.h> int main() { printf("%zu %zu %zu\n", sizeof(uint8_t), sizeof(uint16_t), sizeof(uint32_t)); return 0; }
Attempts:
2 left
💡 Hint
Fixed-width integers have sizes guaranteed by their names.
✗ Incorrect
uint8_t is 8 bits = 1 byte, uint16_t is 16 bits = 2 bytes, and uint32_t is 32 bits = 4 bytes.🔧 Debug
advanced2:00remaining
Unexpected output due to integer promotion
This code intends to add two
uint8_t variables and print the result. What is the output and why?Embedded C
#include <stdio.h> #include <stdint.h> int main() { uint8_t x = 200; uint8_t y = 100; uint8_t z = x + y; printf("%u\n", z); return 0; }
Attempts:
2 left
💡 Hint
Remember that arithmetic on small integer types is promoted to int before assignment.
✗ Incorrect
The sum 200 + 100 = 300 exceeds the max value of uint8_t (255). When assigned back to uint8_t, it wraps modulo 256, so 300 - 256 = 44 is stored and printed.
🧠 Conceptual
advanced1:30remaining
Range of uint16_t
What is the range of values that a
uint16_t variable can hold?Attempts:
2 left
💡 Hint
uint16_t is unsigned and 16 bits wide.✗ Incorrect
uint16_t is an unsigned 16-bit integer, so it holds values from 0 up to 2^16 - 1 = 65535.❓ Predict Output
expert2:30remaining
Bitwise operations on uint32_t
What is the output of this code snippet?
Embedded C
#include <stdio.h> #include <stdint.h> int main() { uint32_t a = 0xF0F0F0F0; uint32_t b = 0x0F0F0F0F; uint32_t c = (a & b) | (~a & b); printf("0x%X\n", c); return 0; }
Attempts:
2 left
💡 Hint
Simplify the expression using bitwise logic identities.
✗ Incorrect
The expression (a & b) | (~a & b) simplifies to b because (a & b) | (~a & b) = b & (a | ~a) = b & 1s = b.