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
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
Many C functions return NULL when something goes wrong. For example, fopen() returns NULL if a file cannot be opened, and malloc() returns NULL if memory allocation fails.
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 - What is the difference between NULL, '\0' and 0? - Stack Overflow
Some commenters have expressed doubt that NULL be equal to 0, but not be zero. Here is an example program, along with expected output on such a system: More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Why are there two ways of expressing NULL in C? - Stack Overflow
Both are forms of a null pointer constant in the C language. They can be used interchangeably for the purpose. Moreover, not only the specific integer constant expression 0 can serve in this role, but any integer constant expression with value 0 can do. For example, 1 + 2 + 3 + 4 - 10 is such ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is a null statement in C? - Stack Overflow
I want to know exactly, what is a null statement in C programming language? And explain a typical use of it. I found the following segment of code. ... An example might be while (1 == 1) ; which loops forever doing nothing. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
January 10, 2025 - Examples ยท Quizzes ยท Projects ... location but NULL. According to C11 standard: โ€œAn integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant....
๐ŸŒ
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 - The incorrectUse() function directly dereferences the pointer without checking if it's NULL. This results in undefined behavior because we're attempting to access memory that doesn't exist. This example shows why it is important to check if the NULL pointer was successfully created in dynamic memory allocation.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - ... That โ€˜\0โ€˜ at the end helps functions like printf(), strlen(), strcpy() know where to stop reading the string. ... Both are valid, but the string literal version is cleaner and more common.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_null_pointer.htm
NULL Pointer in C
The malloc() and calloc() functions are used to dynamically allocate a block of memory. On success, these functions return the pointer to the allocated block; whereas on failure, they return NULL. The following example shows how you can use the NULL pointer to check whether memory allocation ...
๐ŸŒ
cppreference.com
en.cppreference.com โ€บ c โ€บ types โ€บ NULL
NULL - cppreference.com
#include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(void) { // any kind of pointer can be set to NULL int* p = NULL; struct S *s = NULL; void(*f)(int, double) = NULL; printf("%p %p %p\n", (void*)p, (void*)s, (void*)(long)f); // many pointer-returning functions ...
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - A null pointer refers to the 0th memory location, which is a shared memory area that can't be dereferenced. In the example below, We create a pointer *ptr and assign it the value NULL to indicate that it doesn't point to any variables.
๐ŸŒ
CodeWithHarry
codewithharry.com โ€บ tutorial โ€บ c-null-pointer
NULL Pointer | C Tutorial | CodeWithHarry
NULL pointers and void pointers ... a pointer of void data type. Their significances are contrasting. An example of a NULL pointer is as follows: int *ptr = NULL; Here, an integer pointer variable is declared with a value NULL, which means it is not pointing to any memory ...
Top answer
1 of 11
445

Note: This answer applies to the C language, not C++.


Null Pointers

The integer constant literal 0 has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0, it is just described in different ways.

If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 cast to the type void * is both a null pointer and a null pointer constant.

Additionally, to help readability, the macro NULL is provided in the header file stddef.h. Depending upon your compiler it might be possible to #undef NULL and redefine it to something wacky.

Therefore, here are some valid ways to check for a null pointer:

if (pointer == NULL)

NULL is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL is, as long as it is a valid null pointer constant.

if (pointer == 0)

0 is another representation of the null pointer constant.

if (!pointer)

This if statement implicitly checks "is not 0", so we reverse that to mean "is 0".

The following are INVALID ways to check for a null pointer:

int mynull = 0;
<some code>
if (pointer == mynull)

To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.

Note that the value of a null pointer in the C language does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.

As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:

if (!pointer)
if (pointer == NULL)
if (pointer == 0)

The following are INVALID ways to check for a null pointer:

#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)

as these are seen by a compiler as normal comparisons.

Null Characters

'\0' is defined to be a null character - that is a character with all bits set to zero. '\0' is (like all character literals) an integer constant, in this case with the value zero. So '\0' is completely equivalent to an unadorned 0 integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").

'\0' has nothing to do with pointers. However, you may see something similar to this code:

if (!*char_pointer)

checks if the char pointer is pointing at a null character.

if (*char_pointer)

checks if the char pointer is pointing at a non-null character.

Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.

References

See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.

2 of 11
45

It appears that a number of people misunderstand what the differences between NULL, '\0' and 0 are. So, to explain, and in attempt to avoid repeating things said earlier:

A constant expression of type int with the value 0, or an expression of this type, cast to type void * is a null pointer constant, which if converted to a pointer becomes a null pointer. It is guaranteed by the standard to compare unequal to any pointer to any object or function.

NULL is a macro, defined in as a null pointer constant.

\0 is a construction used to represent the null character, used to terminate a string.

A null character is a byte which has all its bits set to 0.

๐ŸŒ
Wikihow
wikihow.com โ€บ computers and electronics โ€บ software โ€บ programming โ€บ c programming languages โ€บ how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - It's common practice to set newly created or newly freed pointers to NULL to make sure you don't use this unhelpful address by accident. Avoid this mistake: char *ptr; if(ptr == NULL) { //This will return FALSE.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ difference-between-null-pointer-null-character-0-and-0-in-c-with-examples
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples - GeeksforGeeks
July 15, 2025 - This 0 is then referred to as a null pointer constant. The C standard defines that 0 is typecast to (void *) is both a null pointer and a null pointer constant. The macro NULL is provided in the header file "stddef.h".
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - The null pointer is a pointer that intentionally points to nothing. If you don't have an address to assign to a pointer, you can use null. The null value avoids memory leaks and crashes in applications that contain pointers. An example of a null pointer in C is:
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.

๐ŸŒ
Handmade Network
hero.handmade.network โ€บ forums โ€บ code-discussion โ€บ t โ€บ 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
The concept of a pointer is just a semantic construct. If we look at nullptr, what exactly is it? Well, we don't know without doing a google search or looking up some kind of specification. Suddenly nullptr becomes a little more abstracted when nullptr is used. ... 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.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ c-null
A Guide to NULL in C - Sabe.io
February 15, 2022 - When you want to initialize a pointer but don't yet have a value, you can use NULL.