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);
return0;
}
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.
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);
return0;
}
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);
return0;
}
Output (verified)
255 4294967295 -1
Q5. What is the difference between int_least8_t, int_fast8_t and int8_t?Intermediate+
Type
Meaning
Always present?
int8_t
Exactly 8 bits, 2’s complement, no padding
No (optional)
int_least8_t
Smallest type with at least 8 bits
Yes
int_fast8_t
Fastest 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
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.
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.
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
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 3.3: sizeof and type casting.
70–89%
Good
Redo Examples 2, 5, 6 and 7.
Below 70%
Revise
Re-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.
2 Comments