ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: A. 5 1

q was assigned a COPY of p's original value (the address of a) before p was set to NULL, so q is unaffected and still validly points at a; *q is 5. p itself is now NULL, so p == NULL is true (1).

More C Programming questions

Exams that ask this topic