ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
void f(void) {
    register int i;
    int *p = &i;
    *p = 5;
    printf("%d", i);
}
  1. A. Compile-time error: cannot take the address of a register variable
  2. B. 5, with the compiler simply ignoring the register hint for this variable
  3. C. 0
  4. D. Undefined behaviour

Correct answer: A. Compile-time error: cannot take the address of a register variable

register is a hint to the compiler suggesting the variable might be stored in a CPU register instead of memory; the standard explicitly forbids taking the address of a variable declared register, precisely because a register has no memory address. Modern compilers enforce this as a hard compile-time error (and in C++, register is deprecated/removed entirely), even though many compilers ignore the hint for optimisation purposes elsewhere.

More C Programming questions

Exams that ask this topic