ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int a = 10;
int *p = &a;
int *q = p;
p = NULL;
printf("%d", *q);
  1. A. 0
  2. B. Undefined behavior: p is NULL
  3. C. 10
  4. D. Compilation error

Correct answer: C. 10

q was assigned a COPY of p's value (the address of a) before p was set to NULL; q still holds &a, which is perfectly valid, so *q reads a = 10. Setting p to NULL does not retroactively affect q.

More C Programming questions

Exams that ask this topic