ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
void f(void) {
register int i;
int *p = &i;
*p = 5;
printf("%d", i);
}- A. Compile-time error: cannot take the address of a register variable
- B. 5, with the compiler simply ignoring the register hint for this variable
- C. 0
- 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
- What is printed? c int x = 100; void f(void) { extern int x; printf("%d", x); } int main(void) { f(); }
- What is printed? c void f(void) { printf("%d ", x); } int x = 42; int main(void) { f(); }
- What is printed? c int x = 5; void modify(void) { x = 10; } int main(void) { int x = 20; modify(); printf("%d %d", x, ::x == 5); }
- What is printed? c static int helper(int x) { return x 2; } int main(void) { printf("%d", helper(5)); }
- What is printed? c void outer(void) { int x = 1; void inner(void) { printf("%d", x); } inner(); } int main(void) { outer(); }
- What happens at link time? c // file1.c static int counter = 0; void increment(void) { counter++; } c // file2.c (compiled and linked with…