C standard macros: in which standard header(s) is the macro NULL guaranteed to be defined?

Difficulty: Medium

Correct Answer: stdio.h and stddef.h

Explanation:

Introduction / Context:NULL is the canonical macro used to denote a null pointer constant in C. Many standard headers include its definition so that programmers can use NULL without memorizing which specific header introduced it in a particular translation unit.

Given Data / Assumptions:

  • We are targeting conforming hosted C implementations.
  • We want to know which headers are required by the C standard to provide NULL.

Concept / Approach:The C standard specifies that several headers define NULL, including , , , , and . Therefore, both stddef.h and stdio.h provide it. Choosing either header (or many others) makes NULL available.

Step-by-Step Solution:Identify headers guaranteed to define NULL.Confirm that both stddef.h and stdio.h are among them.Select the option that lists both together.

Verification / Alternative check:Compile small files that include only stddef.h or only stdio.h and use NULL in a declaration; both compile successfully on conforming implementations.

Why Other Options Are Wrong:

  • stdio.h alone or stddef.h alone omit one valid header from the paired choice.
  • math.h is not required to define NULL.

Common Pitfalls:Confusing NULL (a null pointer constant) with 0 or with the C++ nullptr keyword; assuming only one header provides NULL.

Final Answer:stdio.h and stddef.h.

More Questions from Pointers

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion