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
🌐
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. Following are some most common uses of the NULL pointer in C:
Published   January 10, 2025
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

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 do we use Null pointers?
The value where a null pointer is pointing is irrelevant. A null pointer is a pointer that is pointing nowhere--there is no value. In C, the number 0 was defined as the representation of the null pointer. They could have picked any number, but it would have been painful. More on reddit.com
🌐 r/C_Programming
56
0
August 30, 2024
What is Null?
This was intended as a meme but is actually a good representation of what "Null" is. In C#, when you declare string s = "My shit"; it means that "s" is a reference to a memory location that holds the data "My shit". string s = null; means that the reference "s" exists but it's not pointing to any object, as in it holds nothing. More on reddit.com
🌐 r/learnprogramming
60
34
July 5, 2024
What does !NULL means in C?
"NULL" is implementation defined, the standard says it can be defined as an integer equal to 0, an integer equal to 0 cast to (void*), or anything equal to that (so a definition of 0L is valid, apparently). Though in this case it doesn't really matter what NULL itself is defined as because the "!" operator checks if something is equal to 0 (in any method) and if-so will return an integer 1. So !NULL always is equal to an integer set to 1, irregardless of the implementation of NULL. More on reddit.com
🌐 r/C_Programming
15
0
August 29, 2022
People also ask

What is a null pointer in C?
A null pointer in C is a pointer that does not point to any valid memory location and is assigned the value NULL.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
How to check if a pointer is NULL in C?
You can check whether a pointer is NULL by using an if condition before accessing it to ensure it points to valid memory. For example, if (ptr == NULL) checks whether the pointer is empty before using it.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
What is the value of a null pointer in C?
The value is NULL, which is usually defined as ((void *)0) or 0, depending on the system.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
For example, fopen() returns NULL if a file cannot be opened, and malloc() returns NULL if memory allocation fails. We can check for this using an if statement, and print an error message if something goes wrong. In this example, we try to open a file that does not exist. Since fopen() fails, it returns NULL and we print an error message:
🌐
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 first pointer intPtr is an integer type pointer, and floatPtr is a floating-point type pointer. We initialize them both to NULL, which indicates that neither pointer currently points to any valid memory location. We then use two separate if-statements to check if each pointer is equal to NULL, using the relational equality operator.
🌐
cppreference.com
en.cppreference.com › c › types › NULL
NULL - cppreference.com
January 12, 2024 - #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 use null pointers to indicate error char *ptr = malloc(0xFULL); if (ptr == NULL) printf("Out of memory"); else printf("ptr = %#" PRIxPTR"\n", (uintptr_t)ptr); free(ptr); }
🌐
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.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - 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. We add a condition after creating a pointer variable that checks if the value of the pointer is null.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_null.htm
C library - NULL Macro
#include <stddef.h> #include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp != NULL ) { printf("Opend file file.txt successfully\n"); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("Could not open file nofile.txt\n"); } return(0); } Assume that we have an existing file file.txt but nofile.txt does not exist. By compiling the above program, we get the following result − · Opend file file.txt successfully Could not open file nofile.txt
🌐
Sabe
sabe.io › blog › c-null
A Guide to NULL in C - Sabe.io
February 15, 2022 - C, uses NULL. While null is usually used to represent the absence of a value, in C, it is used to represent a null pointer.
🌐
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 * ...
🌐
LabEx
labex.io › tutorials › c-using-null-pointer-in-c-programming-123293
Using Null Pointer in C Programming
To declare a null pointer, you can assign the value NULL to a pointer variable. #include <stdio.h> int main() { int *ptr = NULL; // ptr is a NULL pointer return 0; } A void pointer is a pointer that has no specific type.
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Null-Pointers.html
Null Pointers (GNU C Language Manual)
You can also explicitly cast NULL to the specific pointer type you want—it makes no difference. ... Since testing a pointer for not being null is basic and frequent, all but beginners in C will understand the conditional without need for != NULL:
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
To check for a null pointer before accessing any pointer variable so that we can perform error handling in pointer-related code. For example, dereference a pointer variable only if it's not NULL. A NULL pointer is always used to detect the endpoint of trees, linked lists, and other dynamic ...
🌐
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
Answer (1 of 4): Question: What are all ways a null is used in C and C++ besides a null pointer? 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 ...
🌐
PrepBytes
prepbytes.com › home › c programming › null character in c
Null Character in C
August 3, 2023 - It is not the same as the character ‘0’ which has an ASCII value of 48. The Null Character in C is used to denote the end of a C string, indicating that there are no more characters in the sequence after it.
🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - In C#, null means "no object." Information about null and its usages in C# include: You cannot use 0 instead of null in your programs even though null is represented by the value 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 - Since testing for (in)equality treats the operands symmetrically, you can get exactly the same result by writing if (NULL == ptr) instead. This is more typo-resistant, since an accidental NULL = ptr creates a simple compile error. This looks a little awkward to some programmers, but it's perfectly valid. Which approach you use just depends on personal preference, and how good your compiler is at detecting the if (ptr = NULL) error.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - This program creates a string ... the digit zero. It has an ASCII value of 48. ... NULL: A macro that represents a null pointer, usually defined as ((void*)0). ‘\0’: The null character used to end strings...