ProgrammingHardHCL · Tech Mahindra pattern

What happens when this runs?

What happens when this runs?
int *p = NULL;
printf("%d", *p);
  1. A. 0, since dereferencing NULL always reads 0
  2. B. Undefined behaviour: dereferencing a null pointer
  3. C. Compile-time error: cannot dereference NULL
  4. D. It always crashes with a segmentation fault

Correct answer: B. Undefined behaviour: dereferencing a null pointer

Dereferencing a null pointer is undefined behaviour per the C standard. In practice it very often crashes (segfault) on common OSes because address 0 is unmapped, but the standard does not require a crash — it could also appear to 'succeed' and read garbage on some embedded or unusual platforms, so neither of the other options is guaranteed.

More C Programming questions

Exams that ask this topic