#define NULL ( (void *) 0)

and

#define NULL 0

are both valid. If you need to implement your own macro for null pointer, the same rule applies.

C11(ISO/IEC 9899:201x) ยง6.3.2.3 Pointers Section 3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant

Answer from Yu Hao on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
โ€œ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 ...
Published ย  January 10, 2025
Discussions

Why is there a NULL in the C language? - Stack Overflow
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
NULL Define in c & c++ differs?! | Handmade Network
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 ... More on hero.handmade.network
๐ŸŒ hero.handmade.network
c - What is the difference between NULL, '\0' and 0? - Stack Overflow
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
You can compare a pointer to NULL to check if it is safe to use. 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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_macro_null.htm
C library - NULL Macro
Following is the C library syntax of the NULL Macro. #define NULL ((char *)0) or, #define NULL 0L or #define NULL 0 ยท This is not a function. So, it doesn't accept any parameter.
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - The C and C++ programming, a pointer is a variable that holds a memory location. 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.
๐ŸŒ
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 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.

Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_null_pointer.htm
NULL Pointer in C
This is how you would declare and initialize a NULL pointer โˆ’ ... #include <stdio.h> int main() { int *p= NULL;//initialize the pointer as null. printf("The value of pointer is %u",p); return 0; } When you run this code, it will produce the ...
๐ŸŒ
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*.
๐ŸŒ
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 constant, typically 0 or ((void *)0) (see also question 5.6).
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching โ‚น23L. ... We can assign 0 directly to the pointer so that it will not point to any of the memory locations. ... Explanation: In the above example, we initialized three pointers as *ptr1, *ptr2, and *ptr3, and we assigned a value to the num variable in *ptr1 and compared it by 0 because *ptr1 is not equal to null, therefore it would output the result as NOT NULL, and we did not assign any value to *ptr2 and As a result, the output will be printed as NOT NULL.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
March 18, 2026 - Learn in this tutorial about the null pointer in C, including its syntax, uses, how to check it, best practices, and examples to write efficient programs.
๐ŸŒ
Handmade Network
hero.handmade.network โ€บ forums โ€บ code-discussion โ€บ t โ€บ 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
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.
๐ŸŒ
The Valley of Code
thevalleyofcode.com โ€บ lesson โ€บ c-advanced โ€บ null
C Advanced: NULL values
NULL is not available by default: you need to include stdio.h to use it (or, if you prefer, stddef.h): #include <stdio.h> int main(void) { int * p_some_variable = NULL; } ... hello.c:3:26: error: use of undeclared identifier 'NULL' int * ...
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - NULL: A macro that represents a null pointer, usually defined as ((void*)0). โ€˜\0โ€™: The null character used to end strings. It has an ASCII value of 0. Sanfoundry Global Education & Learning Series โ€“ 1000 C Tutorials.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Null-Pointers.html
Null Pointers (GNU C Language Manual)
Next: Dereferencing Null or Invalid Pointers, Previous: Dereferencing Pointers, Up: Pointers [Contents][Index] 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.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-using-null-pointer-in-c-programming-123293
Using Null Pointer in C Programming
#include <stdio.h> #include <string.h> #define MAX_NAMES 100 int search(char *ptr[], char* name); int main(void) { char *names[MAX_NAMES]; char name[50]; int count = 0; printf("Enter names (press enter to stop):\n"); // get names from user input while(1) { scanf("%s", name); if(strcmp(name, "") == 0) { break; } names[count] = (char*) malloc(strlen(name)+1); strcpy(names[count], name); count++; } names[count] = NULL; // mark end of array with null pointer // search for names while(1) { printf("Enter name to search for (press enter to stop):\n"); scanf("%s", name); if(strcmp(name, "") == 0) { br
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.

๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ old compilers and null
r/C_Programming on Reddit: Old compilers and NULL
March 29, 2022 - Lecturers rarely give out any relevant advice when it comes to programming languages, and they tend to write horrific code, so take everything they say with a grain of salt. ... No compiler defines or recognizes NULL. It's just a macro you can, ...