C CLP study materials
C CLP study resources

C CLP: Data Types, Declarations, and Type Conversions

C CLP · Work through your exam, one topic at a time.

All guide sections

Data Types, Declarations, and Type Conversions

This domain tests whether you can name the type and value of an expression after C's implicit conversions, and whether you know which conversions are undefined rather than merely surprising.

The type system in one pass

  • Integer types: char, short, int, long, long long, each signed or unsigned; _Bool (include <stdbool.h> for bool, true, false). Whether plain char is signed is implementation-defined.
  • <stdint.h> supplies exact-width int32_t, minimum-width int_least32_t, fast types, and intptr_t/uintmax_t. sizeof yields size_t, an unsigned type; pointer subtraction yields ptrdiff_t.
  • Qualifiers: const, volatile, and restrict (pointers only). volatile forbids optimization of the access; it is not a threading primitive.

Integer promotions and the usual arithmetic conversions

  1. Any type of rank lower than int is promoted to int if int can hold all its values, otherwise to unsigned int.
  2. If either operand is long double, double, or float, the other converts to it.
  3. Otherwise, if both operands share a signedness, the lower rank converts to the higher rank.
  4. If the unsigned operand's rank is greater than or equal to the signed one's, the signed operand converts to unsigned.
  5. Otherwise the unsigned operand converts to the signed type if that type can represent all its values; if not, both convert to the unsigned counterpart of the signed type.

Worked example

int i = -1;
unsigned int u = 1;
if (i < u) puts("less"); else puts("not less");

Rule 4 applies: i converts to unsigned int, becoming 4294967295 on a 32-bit unsigned int, so the comparison is false and "not less" prints. The same rule makes sizeof(int) > -1 false, because -1 becomes SIZE_MAX.

Traps

  • Signed integer overflow is undefined; unsigned overflow wraps modulo 2^N.
  • Converting an out-of-range double to an integer is undefined; in-range values truncate toward zero, so (int)-2.9 is -2.
  • char c = getchar(); loses EOF and can pass a negative value to toupper, which is undefined - cast to unsigned char.
  • Removing const through a pointer needs a cast and is only safe when the object is not actually const.
  • printf("%u", i) with an int argument is undefined; match the conversion specifier to the promoted type.