C Programming Basics

In embedded programs, variables that hold the value of a 32-bit peripheral register are often declared with uint32_t from stdint.h rather than unsigned int. Which of the following is the main reason?

  1. Auint32_t does not wrap to 0 when a value exceeds the maximum but stops at the maximum (saturates)
  2. BReads and writes of a variable declared as uint32_t are no longer removed by compiler optimization
  3. CThe size of unsigned int differs between implementations, whereas uint32_t is defined as an unsigned integer of exactly 32 bits
Show answer and explanation

Answer: C. The size of unsigned int differs between implementations, whereas uint32_t is defined as an unsigned integer of exactly 32 bits

uint8_t, uint16_t, uint32_t, int32_t and the like from stdint.h are integer types whose bit widths are fixed as their names say (exact-width integer types). unsigned int may be 16 or 32 bits, so using exact-width types where the width must match a register or where a communication data format is defined keeps the meaning unchanged when the code moves to another CPU. Like other unsigned integer types, uint32_t wraps to the remainder modulo 2³² when it overflows. To keep reads and writes from being removed, volatile is added separately from the type.

  • A. Unsigned integer types wrap back toward 0 on overflow. To stop at the maximum, saturation must be written explicitly.
  • B. Keeping reads and writes from being removed is the job of volatile and has nothing to do with the type uint32_t.
  • C. Correct. It is handled at the same 32-bit width in every implementation.
Practice this topic Back to topic

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More C Programming Basics questions

  1. Which of the following is the output when the code above, which includes a division of integers, is executed?
  2. Which of the following is the output when the code above, which uses prefix and postfix ++, is executed?
  3. Which of the following is the output when the code above, with = written in the condition of an if statement, is executed?
  4. The code above was written to check whether bit 2 of the register value reg is 1. Which of the following is the result when it is executed?
  5. uint8_t is an 8-bit unsigned integer type. Which of the following is the output when the code above is executed?