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.

Answer from dbush on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_macro_null.htm
C library - NULL Macro
The C library NULL Macro represent the value of a null pointer constant that may be defined as ((void*)0), 0 or 0L depending on the compiler vendor. Following is the list of NULL macro structure โˆ’ Following is the C library syntax of the NULL ...
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.

Discussions

c - Correct way of defining NULL and NULL_POINTER? - Stack Overflow
Then, how should we define NULL_POINTER ? I defined it the same in my program and it worked, but I suppose that is just a coincidence: ... You should leave the definition for the implementation. More on stackoverflow.com
๐ŸŒ stackoverflow.com
NULL Define in c & c++ differs?! | Handmade Network
I don't really think it's fair to call one way or another stupid or idiotic; it's largely a matter of opinion. For example I have my own opinion: C defining NULL as ((void*)0) was great because anyone can go lookup the exact definition of what NULL is and see the definition in terms of elementary ... More on hero.handmade.network
๐ŸŒ hero.handmade.network
Understanding difference between 0 and NULL
NULL is a macro that expands to a null pointer constant. 0 is a null pointer constant. That means NULL could just be defined as 0. Put simply, sizeof (NULL) is simply not something you can rely on as having a consistent, implementation-independent meaning โ€” you don't even know what type NULL has. It is entirely possible for sizeof (NULL) to be not equal to sizeof (void *). If you want to know how big a particular pointer type is, use sizeof on that pointer type, or on a value of that pointer type. More on reddit.com
๐ŸŒ r/C_Programming
36
15
July 2, 2023
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
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 โ€บ c โ€บ types โ€บ NULL
NULL - cppreference.com
January 12, 2024 - POSIX requires NULL to be defined as an integer constant expression with the value 0 cast to void*.
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... NULL is a special value that represents a "null pointer" - a pointer that does not point to anything.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ null-crt
NULL (CRT) | Microsoft Learn
October 26, 2022 - Access to this page requires authorization. You can try changing directories. ... NULL is the null-pointer value used with many pointer operations and functions. It's equivalent to 0.
Find elsewhere
๐ŸŒ
Handmade Network
hero.handmade.network โ€บ forums โ€บ code-discussion โ€บ t โ€บ 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
Randy Gaul For example I have my own opinion: C defining NULL as ((void*)0) was great because anyone can go lookup the exact definition of what NULL is and see the definition in terms of elementary expressions, integers and type-casting. This helps make it obvious that pointers are, on the assembly level, just numbers like any other integer.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Null-Pointers.html
Null Pointers (GNU C Language Manual)
A pointer value can be null, which means it does not point to any object. The cleanest way to get a null pointer is by writing NULL, a standard macro defined in stddef.h. You can also do it by casting 0 to the desired pointer type, as in (char *) 0.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Zero and NULL and Pointers and Stuff | C For Dummies Blog
April 17, 2021 - NULL is used where a pointer, a memory address, is to be evaluated or set as an argument.
๐ŸŒ
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
๐ŸŒ
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 - Here we set p to NULL and explicitly check it. Trying to do *p = 42 (as shown in the comment) would typically result in a segmentation fault. C provides no automatic protection โ€“ it trusts the programmer. The definition of NULL as 0 has a handy side effect: you can use if (p) or if (!p) as a shorthand to check non-null or null, since in C an expression like if (p) is true if p is not zero.
๐ŸŒ
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 - You can initialize a null pointer in C, simply by assigning the special value NULL to a pointer variable. NULL is a macro defined in the standard C libraries, typically as ((void *)0), although its exact definition can vary across implementations.
Top answer
1 of 9
55

Let's consider this example code:

#include <stddef.h>
int *f(void) { return NULL; }
int g(int x) { return x == NULL ? 3 : 4; }

We want f to compile without warnings, and we want g to cause an error or a warning (because an int variable x was compared to a pointer).

In C, #define NULL ((void*)0) gives us both (GCC warning for g, clean compile for f).

However, in C++, #define NULL ((void*)0) causes a compile error for f. Thus, to make it compile in C++, <stddef.h> has #define NULL 0 for C++ only (not for C). Unfortunately, this also prevents the warning from being reported for g. To fix that, C++11 uses built-in nullptr instead of NULL, and with that, C++ compilers report an error for g, and they compile f cleanly.

2 of 9
34

((void *)0) has stronger typing and could lead to better compiler or static analyser diagnostics. For example since implicit conversions between pointers and plain integers aren't allowed in standard C.

0 is likely allowed for historical reasons, from a pre-standard time when everything in C was pretty much just integers and wild implicit conversions between pointers and integers were allowed, though possibly resulting in undefined behavior.

Ancient K&R 1st edition provides some insight (7.14 the assignment operator):

The compilers currently allow a pointer to be assigned to an integer, an integer to a pointer, and a pointer to a pointer of another type. The assignment is a pure copy operation, with no conversion. This usage is nonportable, and may produce pointers which cause addressing exceptions when used. However, it is guaranteed that assignment of the constant 0 to a pointer will produce a null pointer distinguishable from a pointer to any object.

๐ŸŒ
Medium
medium.com โ€บ @pauljlucas โ€บ nullptr-in-c23-571782008dad
nullptr in C23. The new nullptr keyword in C23. | by Paul J. Lucas | Medium
July 8, 2025 - Since its creation in the 1970s, C has used the preprocessor to define a macro for the โ€œnull pointer.โ€ Originally, it was defined as: ... or perhaps 0L since void and the ability to use void* werenโ€™t added until C89. This required C to have a special case for the integer literals 0 and 0L (and 0LL once long long was added in C99, as well as their unsigned variants) allowing only them to be converted to any pointer type implicitly: ... For simplicity, going forward, whenever I mention 0 as a possible definition of NULL, interpret that to mean either 0 or any one of 0U, 0L, 0UL, 0LL, or 0ULL.