Quiz: stdint.h Interview Questions for Embedded C: Fixed-Width Types Quiz-Free Embedded C Tutorial

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.2 · PRACTICE

stdint.h Interview Questions for Embedded C: Fixed-Width Types Quiz

69 questions: multiple choice, fill-in-the-blank, a choose-the-type table, range calculations, true/false, 6 code snippet challenges and 12 detailed interview answers.

69
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0
Correct
0
Wrong
0
Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.

Section 1: Multiple Choice Questions — stdint.h and Fixed-Width Types

These stdint.h interview questions in multiple-choice form cover ranges, mappings and printing. Click your answer; explanations always appear.

1. Which header defines uint8_t and int32_t?

2. What is the range of uint16_t?

3. What is the range of int8_t?

4. Why are fixed-width types preferred for register definitions?

5. On arm-none-eabi-gcc with newlib, uint32_t is a typedef of:

6. The portable way to print a uint32_t is:

7. What does UINT32_MAX equal?

8. What is (uint8_t)(250 + 10)?

9. Which type is guaranteed to hold a pointer value as an integer?

10. Which is the smallest type with at least 16 bits and always available?

11. Why write (uint32_t)p[3] << 24 instead of p[3] << 24?

12. A 12-bit ADC value should be stored in:

13. What size is struct { uint8_t a; uint32_t b; } on ARM Cortex-M4?

14. Which macro makes a 64-bit unsigned constant equal to 1?

15. Why is volatile used in volatile uint32_t MODER;?

16. Which 32-bit tick comparison is wrap-safe?

Section 2: Fill in the Blanks

Spaces and capitalisation are ignored. You may type the minus sign as -.

1. UINT8_MAX equals

2. INT16_MIN equals

3. INT32_MAX equals

4. sizeof(uint64_t) is bytes

5. The header that provides PRIu32 is

6. uint16_t x = 65535; x++; makes x equal

7. A uint32_t millisecond tick wraps after about days (whole number)

8. The maximum of uint32_t in hex is 0x

9. Extract bits [11:8] of reg: (reg >> 8) & 0xU

10. The signed type exactly 16 bits wide is (type name)

11. The suffix that makes the literal 1 unsigned is (one letter)

12. sizeof(GPIO_TypeDef) for ten uint32_t members is bytes

Section 3: Choose the Correct Type

A staple of stdint.h interview questions: select the best fixed-width type for each use case on the STM32F407.

<td id=”tyr5″ data-why=”Object sizes and indexes use size_t.” style=”padding:10px 14px;border:1px solid #e2e8f0;font-size:0.9em;”>
Use caseYour answerResult
Value read from a 32-bit peripheral registeruint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Byte received on UARTuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Gyroscope reading, signed 16 bitsuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Sum of 1000 values from a 12-bit ADCuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Microseconds since boot, running for yearsuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Index into a 200-element array of structsuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Address stored as an integeruint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
PWM duty value 0..999uint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t
Temperature −40..+125 °C whole degreesuint8_tint8_tuint16_tint16_tuint32_tuint64_tsize_tuintptr_t

Section 4: Range Calculations

Range calculations appear in many stdint.h interview questions. Work these out on paper first.

13. How many distinct values does a uint16_t hold?

14. Largest signed value in 24 bits:

15. Smallest signed value in 12 bits:

16. How many bytes does uint64_t arr[10] use?

17. Bits needed to store 0..1000:

18. Maximum value of a uint8_t plus 1, stored in uint8_t:

Section 5: True or False

Quick true/false checks, the opening round of many stdint.h interview questions.

1. uint32_t is exactly 32 bits on every compiler that defines it.

2. The exact-width types are mandatory on every C99 compiler.

3. uint32_t is always a typedef of unsigned int.

4. int_fast16_t is always exactly 16 bits.

5. uint8_t a = 0xF0; ~a == 0x0F is true.

6. Unsigned subtraction of two 32-bit ticks gives a correct elapsed time even if the counter wrapped between them.

7. 1 << 31 is well-defined in C because it fits in 32 bits.

8. PRIu64 must be used with printf to print a uint64_t portably.

Section 6: Code Snippet Challenges

Code-reading tasks round out the stdint.h interview questions. Every answer shown was produced by compiling and running the code.

Challenge 1 — Output?: Promotion versus truncation
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t a = 250, b = 10; uint8_t c = a + b; printf(“%u %u\n”, c, a + b); return 0; }

What two numbers are printed?

Output:
4 260
a + b is evaluated as int (260); only the assignment to uint8_t c truncates it to 4.
Challenge 2 — Find the bug: The sum that overflowed
#include <stdio.h> #include <stdint.h> int main(void) { uint16_t samples[4] = { 20000, 20000, 20000, 20000 }; uint16_t sum = 0; for (int i = 0; i < 4; i++) sum += samples[i]; printf(“average = %u\n”, sum / 4); return 0; }

Expected average: 20000. What is printed and what is the fix?

Output:
average = 3616
The sum 80000 does not fit in 16 bits (80000 mod 65536 = 14464; 14464 / 4 = 3616). Fix: uint32_t sum.
Challenge 3 — Complete: Extract a 4-bit field
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t reg = 0x00000A00u; uint32_t field = /* ??? */; printf(“field = 0x%X\n”, (unsigned)field); return 0; }

Replace /* ??? */ so the program prints field = 0xA.

Answer: (reg >> 8) & 0xFU
field = 0xA
Shift the field to bit 0, then mask its width (4 bits = 0xF).
Challenge 4 — Output?: Subtracting unsigned ticks
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t a = 5, b = 10; printf(“%d %d\n”, (a – b) > 0, (int32_t)(a – b) > 0); return 0; }

What does this print?

Output:
1 0
a - b is unsigned: 5 − 10 wraps to 4294967291, which is > 0. Casting the difference to int32_t reveals it is −5. Wrap-safe timeout code uses the unsigned difference compared against a timeout, or the signed difference to test ordering.
Challenge 5 — Output?: Order of members matters
#include <stdio.h> #include <stdint.h> struct P { uint8_t a; uint16_t b; uint8_t c; }; struct Q { uint16_t b; uint8_t a; uint8_t c; }; int main(void) { printf(“%zu %zu\n”, sizeof(struct P), sizeof(struct Q)); return 0; }

What are the two sizes?

Output:
6 4
struct P: a at 0, 1 pad byte, b at 2, c at 4, 1 trailing pad byte = 6. struct Q: b at 0, a at 2, c at 3 = 4. Sorting members from large to small avoids padding.
Challenge 6 — Output?: Sign extension versus zero extension
#include <stdio.h> #include <stdint.h> #include <inttypes.h> int main(void) { int8_t s = –1; uint8_t u = (uint8_t)s; uint32_t w = (uint32_t)s; int32_t ws = s; printf(“%” PRIu8 ” %” PRIu32 ” %” PRId32 “\n”, u, w, ws); return 0; }

What are the three numbers?

Output:
255 4294967295 -1
Casting int8_t −1 to uint8_t keeps the byte (255). Casting it to uint32_t sign-extends first (0xFFFFFFFF = 4294967295), and assigning to int32_t preserves −1.

Section 7: stdint.h Interview Questions for Embedded C (with Detailed Answers)

These stdint.h interview questions are typical of firmware interviews. Click a question for the model answer.

Q1. What are fixed-width integer types and where are they defined?Basic+

They are integer types whose size in bits is encoded in the name (uint8_t, int16_t, uint32_t, int64_t). They are defined in <stdint.h> (C99) as typedefs onto the standard type that has exactly that size on the current compiler.

Q2. Why do embedded programmers prefer uint32_t to unsigned int?Basic+

unsigned int is only guaranteed to be at least 16 bits. A hardware register, a protocol field or a stored file format has a specific width, so the code should state it. uint32_t documents the intent and keeps register layouts and structure sizes identical when the code is rebuilt for another target or tested on a PC.

Q3. Is uint32_t the same type as unsigned int?Basic+

Usually the same size but not necessarily the same type. On a PC with glibc it is unsigned int; with arm-none-eabi newlib it is unsigned long. The practical effect is that printf("%u", x) compiles cleanly on one and warns on the other, and that function overloading or _Generic selection can differ. Use PRIu32 for printing.

Q4. How do you print uint8_t, uint32_t and uint64_t portably?Intermediate+

Include <inttypes.h> and use the format macros through string concatenation: printf("%" PRIu8 " %" PRIu32 " %" PRIu64 "\n", a, b, c);. For limited libraries (newlib-nano without %lld), cast to unsigned long and use %lu, accepting that 64-bit values need splitting into two 32-bit halves.

print_portable.c
#include <stdio.h> #include <stdint.h> #include <inttypes.h> int main(void) { int8_t s = –1; uint8_t u = (uint8_t)s; uint32_t w = (uint32_t)s; int32_t ws = s; printf(“%” PRIu8 ” %” PRIu32 ” %” PRId32 “\n”, u, w, ws); return 0; }
Output (verified)
255 4294967295 -1
Q5. What is the difference between int_least8_t, int_fast8_t and int8_t?Intermediate+
TypeMeaningAlways present?
int8_tExactly 8 bits, 2’s complement, no paddingNo (optional)
int_least8_tSmallest type with at least 8 bitsYes
int_fast8_tFastest type with at least 8 bits (may be 32 bits)Yes

Use the exact type for storage and external interfaces, the fast type for local loop variables in code that must be portable and quick, and the least type when portability to unusual CPUs matters more than exact size.

Q6. Why are peripheral registers declared volatile uint32_t?Intermediate+

uint32_t gives the exact register width and layout. volatile tells the compiler that the value can change outside program control and that every read and write has a side effect, so it must not cache the value in a CPU register, merge accesses or remove “useless” writes. Without it, a polling loop such as while (!(USART2->SR & TXE)) could be optimised into an infinite loop or removed entirely.

uart_block.c — size and offset checked at compile time
#include <stdio.h> #include <stdint.h> #include <stddef.h> typedef struct { volatile uint32_t CR; volatile uint32_t SR; volatile uint32_t DR; } UART_TypeDef; _Static_assert(sizeof(UART_TypeDef) == 12, “register block size”); _Static_assert(offsetof(UART_TypeDef, DR) == 8, “DR offset”); int main(void) { printf(“UART block: %zu bytes, DR at +%zu\n”, sizeof(UART_TypeDef), offsetof(UART_TypeDef, DR)); return 0; }
Output (verified)
UART block: 12 bytes, DR at +8
Q7. How do you implement a timeout that survives the 32-bit tick wrapping around?Intermediate+

Store the start tick and test the difference using unsigned arithmetic: (uint32_t)(now - start) >= timeout. Modular arithmetic yields the true elapsed time provided it is less than 232 ms. Never compute start + timeout and compare against now: that fails when the sum wraps.

timed_out.c
#include <stdio.h> #include <stdint.h> static uint32_t now_ms; /* returns 1 if ‘timeout’ ms have passed since ‘start’, wrap-safe */ static int timed_out(uint32_t start, uint32_t timeout) { return (uint32_t)(now_ms – start) >= timeout; } int main(void) { now_ms = 4294967000u; uint32_t t0 = now_ms; now_ms += 200; printf(“after 200 ms: %d\n”, timed_out(t0, 500)); now_ms += 400; printf(“after 600 ms: %d (now_ms wrapped to %u)\n”, timed_out(t0, 500), now_ms); return 0; }
Output (verified)
after 200 ms: 0 after 600 ms: 1 (now_ms wrapped to 304)
Q8. Why should you cast to uint32_t before shifting a byte left by 24?Intermediate+

A uint8_t operand is promoted to int (signed) before the shift. For a byte value of 0x80 or higher, b << 24 sets the sign bit of a 32-bit int, which is undefined behaviour and can generate sign-extended results when assigned to a wider type. Casting first makes the shift happen in an unsigned 32-bit type, where it is well-defined.

Q9. What if the platform has no uint8_t? Are the exact-width types guaranteed?Advanced+

No. The standard requires the exact-width types only if the implementation has a type with exactly that width, no padding bits and (for signed) 2’s complement. On a TI C2000 DSP, for instance, CHAR_BIT is 16, so sizeof(int8_t) cannot exist and uint8_t is absent. For code that must also run there, use uint_least8_t and mask explicitly (x & 0xFFu). On all Cortex-M devices every exact-width type exists.

Q10. Why is casting a byte buffer to uint32_t * to read a word usually a bug?Advanced+

Three independent problems: alignment (a misaligned word access can fault or be slow, and is undefined behaviour in C), endianness (the result depends on host byte order) and strict aliasing (reading a uint8_t array through a uint32_t * violates the aliasing rules, so the optimiser may reorder or drop accesses). Assemble bytes with shifts, or use memcpy into a uint32_t.

read_be32.c
#include <stdio.h> #include <stdint.h> #include <inttypes.h> static uint32_t read_be32(const uint8_t *p) { return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3]; } int main(void) { uint8_t frame[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; uint32_t v = read_be32(frame); printf(“0x%08” PRIX32 “\n”, v); /* independent of host endianness */ uint32_t direct = *(uint32_t *)frame; /* BAD: depends on endianness and alignment */ printf(“direct cast: 0x%08” PRIX32 ” (little-endian host)\n”, direct); return 0; }
Output on a little-endian PC (verified)
0xDEADBEEF direct cast: 0xEFBEADDE (little-endian host)

The shift-based function always returns the same value; the direct cast returns the bytes in host order (here reversed).

Q11. Why is a bit-field struct a poor way to describe a register compared with masks and shifts on uint32_t?Advanced+

The C standard leaves bit-field allocation order, packing across storage units and alignment implementation-defined, so the same declaration can lay out bits differently on different compilers or endianness. You also cannot take an atomic read-modify-write of a single bit-field member on a peripheral register in a controlled way (the compiler picks the access width). Masks and shifts on a uint32_t are explicit and portable; vendors such as ST and ARM CMSIS therefore define fields as #defined position and mask constants.

Q12. A uint64_t counter is read in an interrupt on a Cortex-M4. What is the hazard?Advanced+

The Cortex-M4 is a 32-bit CPU: a 64-bit load or store takes two instructions, so it is not atomic. If an interrupt updates the counter between the two halves are read, the main code sees a torn value (for example the low half has wrapped but the high half has not been incremented). Protect the read by briefly disabling interrupts, or read the high word, low word, then high again and retry if it changed. Declare the variable volatile as well.

Study Plan: Using Your Score on the stdint.h Interview Questions

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 3.3: sizeof and type casting.
70–89%GoodRedo Examples 2, 5, 6 and 7.
Below 70%ReviseRe-read theory sections 2, 3, 6 and 9, then retake the quiz.
Next lesson: Lesson 3.3 looks at sizeof and at implicit and explicit conversions, including the cast that turns an integer into a register address.

Lesson 3.2 complete!

Next up: the sizeof operator, implicit promotion, explicit casting and casting an integer to a hardware address.

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 *