The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

The C++ standard requires that NULL be defined in the c* header corresponding to each of those.

The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.

The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.

From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).

Answer from Jerry Coffin on Stack Overflow
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - Null is a constant built into C, C++, and C#. It has a value of zero. Null can also be the value of a pointer that points nowhere.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_macro_null.htm
C library - NULL Macro
The C library NULL Macro represent ... may be defined as ((void*)0), 0 or 0L depending on the compiler vendor. ... NULL (void*)0: A null pointer cast to the void* type. NULL 0: An integer literal representing a null pointer.
Discussions

Old compilers and NULL
NULL is a macro that should be defined in a header . In theory it's possible that some compiler ships without a definition for it (can anyone comment if there's a C standard where this is permitted?), in practice you've simply forgotten to include the right header. More on reddit.com
๐ŸŒ r/C_Programming
31
36
March 29, 2022
What header defines NULL in C++? - Stack Overflow
According to C++ primer, header defines NULL. cpluspplus says it is defined in . Ultimately, if the right header is not included, I thought NULL can't be referenced. More on stackoverflow.com
๐ŸŒ stackoverflow.com
NULL Define in c & c++ differs?! | Handmade Network
NULL Define in c & c++ differs?! ... If I use 0(line 49) I get no warning(and that is strange since the book use NULL), if I use NULL(that is what is used in the book, so it should be correct) the compiler says "warning C4047: '!=': 'int' differs in levels of indirection from 'void *'". More on hero.handmade.network
๐ŸŒ hero.handmade.network
Why is there a NULL in the C language? - Stack Overflow
Why is there a NULL in the C language? Is there a context in which just plain literal 0 would not work exactly the same? ... Seeing as NULL is allowed to be defined as 0, then necessarily every instance where NULL appears must, assuming ISO C, work the same if 0 were there. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.โ€ ... We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as โ€œstdio.hโ€, โ€œstddef.hโ€, โ€œstdlib.hโ€ etc.
Published ย  January 10, 2025
๐ŸŒ
cppreference.com
en.cppreference.com โ€บ w โ€บ c โ€บ types โ€บ NULL.html
NULL - cppreference.com
January 12, 2024 - A null pointer constant may be converted to any pointer type; such conversion results in the null pointer value of that type. POSIX requires NULL to be defined as an integer constant expression with the value โ€‹0โ€‹ cast to void*.
๐ŸŒ
C-faq
c-faq.com โ€บ null โ€บ macro.html
Question 5.4
A: As a matter of style, many programmers prefer not to have unadorned 0's scattered through their programs, some representing numbers and some representing pointers. Therefore, the preprocessor macro NULL is defined (by several headers, including <stdio.h> and <stddef.h>) as a null pointer ...
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ null-crt
NULL (CRT) | Microsoft Learn
October 26, 2022 - ... NULL is the null-pointer value used with many pointer operations and functions. It's equivalent to 0. NULL is defined in the following header files: CRTDBG.H, LOCALE.H, STDDEF.H, STDIO.H, STDLIB.H, STRING.H, TCHAR.H, TIME.H and WCHAR.H.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - In the C programming language, a null pointer is a pointer that does not point to any memory location and hence does not hold the address of any variables. It just stores the segment's base address.
๐ŸŒ
Handmade Network
hero.handmade.network โ€บ forums โ€บ code-discussion โ€บ t โ€บ 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
A dangling pointer occurs when the variable that the pointer points to no longer exists. After that, who knows what it points to. A very unsafe practice. So, NULL is used. It is defined as (void *)0 because then, the pointer then points to nothing.
Top answer
1 of 5
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

CopyNULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 5
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question โ€œIs there a context in which just plain literal 0 would not work exactly the same?โ€:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

๐ŸŒ
Quora
quora.com โ€บ What-are-all-ways-a-null-is-used-in-C-and-C-besides-a-null-pointer
What are all ways a null is used in C and C++ besides a null pointer? - Quora
Thatโ€™s it - Because otherwise itโ€™s just 0. In fact in C, NULL is โ€œdefinedโ€ as 0 and if you have a string, theyโ€™re terminated with a null but if you look at a string(character buffer that is), that โ€˜n...
๐ŸŒ
Medium
medium.com โ€บ @shlomohassid โ€บ null-how-do-you-define-nothing-and-why-would-you-07683bdbe63a
NULL: How Do You Define Nothing? And Why Would You? | by Momi | Medium
May 31, 2025 - In C, NULL is a macro that represents a null pointer constant โ€“ typically defined as ((void*)0) or just 0 in standard headers. This means that writing ptr = NULL; in C is the same as ptr = 0; (with a cast to the appropriate pointer type).
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstring โ€บ NULL
NULL
A null-pointer constant is either an integral constant expression that evaluates to zero (such as 0 or 0L), or a value of type nullptr_t (such as nullptr). A null pointer constant can be converted to any pointer type (or pointer-to-member type), which acquires a null pointer value.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - Declaration and Initialization: A null pointer in C is declared and initialized like any other pointer. It's assigned the special value NULL, typically defined as (void *)0 or 0, indicating that it currently points to no valid memory address.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_null_pointer.htm
NULL Pointer in C
A NULL pointer in C is a pointer that doesn't point to any of the memory locations. The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h. A pointer is initialized to NULL to avoid the unpredicted behavior of a
๐ŸŒ
Quora
quora.com โ€บ What-is-Null-Macro-in-C
What is Null Macro in C? - Quora
Answer (1 of 3): Null macro is ... uninitialized pointer..It does not point anywhere. It may be defined as ((void*)0), 0or 0L depending on the compiler vendor. Following......
๐ŸŒ
Linux Man Pages
man7.org โ€บ linux โ€บ man-pages โ€บ man3 โ€บ null.3const.html
NULL(3const) - Linux manual page
It is also undefined behavior to perform pointer arithmetic on it. NULL - NULL is undefined behavior, according to ISO C, but is defined to be 0 in C++. To avoid confusing human readers of the code, do not compare pointer variables to 0, and do not assign 0 to them.