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.

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.

Discussions

In C, why is NULL and 0 triggering an if statement - Software Engineering Stack Exchange
What you are trying to do cannot be done (since NULL and 0 are the same thing). Chances are you are reading the assignment incorrectly. ... I'm voting to close this question as off-topic because it is more appropriate to ask at Stack Overflow. However, it would be closed as a duplicate of What is the difference between ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
June 10, 2016
NULL and 0 in C language - Programming & Development - Spiceworks Community
Does NULL and 0 have equal functionality in C language ?. Thanks. More on community.spiceworks.com
🌐 community.spiceworks.com
0
October 8, 2010
pointers - Is NULL always zero in C? - Stack Overflow
On some systems, 0 may be a perfectly valid memory address accessible by your program, and a different value (such as, say, 0xFFFFFFFF) may be used for "null" . However, within your source code, a 0-valued expression is always used to represent the NULL pointer constant. More on stackoverflow.com
🌐 stackoverflow.com
c - Are there reasons to assign NULL instead of 0 to non-pointer variables? - Software Engineering Stack Exchange
NULL is defined as either 0 or ... means null. 0 is brief and easy to read and can be used anywhere you see NULL or nullptr in C and C++. In languages like Javascript, the null type is something else and it can be quiet useful. In JSON it differentiates between no value ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 23, 2016
🌐
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 - 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". Below are the ways to check for a NULL pointer: NULL is defined to compare equal ...
🌐
C For Dummies
c-for-dummies.com › blog
The Difference Between NULL and Zero | C For Dummies Blog
The NULL pointer holds address zero, which may cause you to flare your nostrils, point at the screen, and shout, “I told you!” But it’s still not a zero in the “Isn’t NULL equal to zero?” sense. The second line demonstrates how the \0 escape character is really value zero:
Top answer
1 of 3
12

In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect).

In the first case, you can not differentiate between NULL and 0, because they are literally the same.
In the second case, your code will cause a compile error, because you can't compare an integer variable with a pointer.

2 of 3
4

First some background ...


The macros are NULL which expands to an implementation-defined null pointer constant; C11 §7.19 3

NULL typically is an integer constant 0 or (void*)0 or the like. It may have a different implementation or type - It could be ((int*) 0xDEADBEEF) as strange as that may be.

NULL might be type int. It might be type void * or something else. The type of NULL is not defined.


When the null pointer constant NULL is cast to any pointer, is is a null pointer. An integer 0 cast to a pointer is also a null pointer. A system could have many different (bit-wise) null pointers. They all compare equally to each other. They all compare unequally to any valid object/function. Recall this compare is done as pointers, not integers.

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. C11 §6.3.2.3 3

int x;
if (&x == NULL) ... // this is false

So after all that chapter and verse how to distinguish NULL from 0?

If the macro NULL is defined as an int 0 - it is game over - there is no difference between 0 and NULL.

If NULL is not an int, then code can use _Generic() to differentiate NULL and 0. This does not help OP's "Any change made can only be made within the function itself." requirement as that function accepts an int augment.

If NULL is an int that has a different bit-pattern than 0, then a simple memcmp() can differentiate.

I suspect the whole reason for this exercise is to realize there is no portable method to distinguish NULL from 0.

🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c interview questions › what is the difference between null and zero?
What is the difference between null and zero? | Fresh2Refresh.com
July 15, 2020 - Learn What is the difference between null and zero? - NULL is a macro which is defined in C header files. The value of NULL macro is 0.
🌐
C For Dummies
c-for-dummies.com › blog
Zero and NULL and Pointers and Stuff | C For Dummies Blog
April 17, 2021 - Instead, think of it as an “empty” location and not zero; NULL indicates the absence of a memory location, which is important when comparing pointers: NULL evaluates as FALSE. The point [sic] of this post is that despite all this NULL nonsense, it’s unwise to assume NULL‘s definition.
Find elsewhere
🌐
DEV Community
dev.to › sundar_joseph_94059a3e7a6 › difference-between-null-pointer-null-character-0-and-0-in-c-with-examples-3o7m
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples - DEV Community
July 22, 2025 - 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". Below are the ways to check for a NULL pointer: NULL is defined to compare equal ...
🌐
Quora
quora.com › What-are-the-differences-between-null-0-and-0-in-the-C-language
What are the differences between null, '\0' and 0 in the C language? - Quora
Answer (1 of 6): NULL: A void pointer NULL should only be used in pointer contexts. Let's see how NULL is defined by the C standard: NULL can be defined in one of two ways: [code]#define NULL 0 [/code]or [code]#define NULL ((void*) 0) [/code]The difference between the two is the first defini...
🌐
Sololearn
sololearn.com › en › Discuss › 345807 › are-null-and-zero-that-same-thing-in-c
Are NULL and zero that same thing in C++? | Sololearn: Learn to code for FREE!
0 treated as number data type value and also treated as false using in a condition. where as null used as special meaning that define a variable hold no value. Most of time, null assigned to object that have no value ...
🌐
Quora
quora.com › IS-null-same-as-0-in-C
IS null same as 0 in C? - Quora
Answer (1 of 2): > IS null same as 0 in C? No I can think of several things that your question could mean, but the answer to all of them is no. The word “null” is an English word that’s used in the C standard document in several different contexts. I’m going to guess that you meant to ask abo...
Top answer
1 of 5
21

The macro NULL is a null-pointer constant and has either an integer type or a pointer type.

Using NULL to assign or initialize a non-pointer variable will lead to question marks from other programmers at the least and it might result in compiler failures.
A line like

int a = NULL;

is not considered good code and it will make the code less readable.

2 of 5
9

I think @R Sahu's answer reaches the right conclusion, but the supporting evidence it provides (based on a single implementation) is somewhat weak, at best.

This is tagged with both c and c++. The details of how NULL is defined vary between the two, and also varies over time for C++. In all cases, NULL must expand to an "implementation defined null pointer constant". What varies is the definition of "null pointer constant".

In C, a null pointer constant must be an integer literal with the value 0, or the same cast to type "pointer to void"1. An integer with a non-zero value (by itself, or cast to type "pointer to void") is not allowed2. So, 0, 0L and ((void *)0) are all allowed, but something like ((void *)1234) is not.

In C++ 98/03, NULL must also expand to a null pointer constant--but with a somewhat different definition of the term--in particular, casting the integer literal to type "pointer to void" is not allowed in C++ 98/03. This means 0 and 0L are both allowed (and so would '\0' or, if you wanted to be really perverse, (3-(2 + 1)).

In C++ 11, the nullptr_t type and nullptr literal were added to C++, and nullptr is also allowed as a null pointer constant3. NULL is allowed to expand to any null pointer constant, so it could be 0, 0L (etc.) or nullptr, but (again) cannot be any non-zero integer, nor can it have type "pointer to void" (or "pointer to char", etc.)

The intent, however, has always been that NULL only be used to represent a null pointer. Although both C and C++ allow it to be defined as an unadorned 0 (for one example), so it might be possible to assign it to a variable of type int, there's no guarantee that code that does so will even compile (and a fair number of people believe that it shouldn't compile).

Conclusion: you should only ever assign NULL to a pointer. For new code, I'd advise using NULL only in C, and using nullptr in C++. For existing C++ code, I'd convert to using nullptr when any other refactoring is being done on that code, but would not usually modify the code solely to change NULL to nullptr (but code that assigns NULL to any non-pointer type should be corrected immediately, in either C or C++).


1. The wording in the C standard (§6.3.2.3/3) reads:

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

The italics (which are in the original) mean that this is considered the definition of that term for this standard.

2. Assigning a null pointer constant to a pointer variable may result in a non-zero value being stored into that variable. Although somewhat unusual, this is perfectly allowable. Even if/when that is the case, however, comparing that variable to a null pointer constant (e.g., NULL or 0) must yield true.

Likewise, an implementation is free to define any number of other integer constants that will produce a null pointer when assigned to a pointer variable. This doesn't affect the requirement on how NULL must be defined.

3. Here, the official wording (from [conv.ptr]) reads:

A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t.

Here again, the italics indicate that this is the official definition of the term for that standard.

🌐
sqlpey
sqlpey.com › c › null-char-zero-c
Distinguishing NULL, '�', and 0 in C Programming - sqlpey
July 25, 2025 - At their heart, these three entities serve different purposes: NULL: Primarily used to indicate that a pointer does not point to any valid memory location. It’s a symbolic representation of a null pointer constant. '\0': Represents the null character, which has a byte value of zero.
Top answer
1 of 2
6

The C standard does not provide for an integer type to represent any values other than integers. There is no null or “NULL” value that means “not any integer value.”

The macro NULL is intended for use with pointers, not integers. It is a null pointer constant. For pointers, NULL is a value used to indicate the pointer is not pointing at any thing (some object or function). It should not be used with integers. You could use NULL with the pointer p to determine whether it points to a structure or not, and you could use it with p->next to determine whether there is another node linked to the structure.

If the structure may or may not contain data, then generally you should create another member of the structure that indicates whether or not it contains data (and/or possibly how much data or what type of data). Alternatively, you can designate some integer value representable in the type of the data member that you will never use for its integer value to be an indicator that there is no data present in the structure.

2 of 2
4

There is a confusion between testing if the pointer to struct node passed to the function is a null pointer and the value of the integer data member of the nodes in the list it points to.

NULL is used in C as a macro to express a null pointer constant. It is used to test for null pointers. 0 is also a null pointer constant in a pointer context. Unlike in SQL, there is no concept of a null value representing no value in C for numeric values.

Here is a modified version of your code:

void ecrire(struct node *p) {
    if (p == NULL) { // if the node pointer is NULL it means list is empty
        printf("no items!\n");
        return;
    }
    struct node *temp = p;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
🌐
Cemetech
cemetech.net › forum › viewtopic.php
Regarding NULL and 0 - Cemetech | Forum | [Comp] C and C++ [Topic]
What I found was that the ANSI ... the professor to get a second opinion on the issue. I got this in response: "You are correct in that NULL is most always 0....
🌐
TheLinuxCode
thelinuxcode.com › home › null pointer vs ‘\\0‘ vs ‘0‘ vs 0 in c (with practical examples)
NULL Pointer vs ‘\\0‘ vs ‘0‘ vs 0 in C (with Practical Examples) – TheLinuxCode
February 11, 2026 - Three language rules do most of the damage here:\n\n1) Character literals have type int\nThat’s why ‘\\0‘ and ‘0‘ can participate in integer expressions and comparisons so freely.\n\n2) Integer constant 0 is special in pointer contexts\nIt becomes a null pointer constant. That’s convenient, but it blurs the boundary between integers and pointers in a way beginners (and tired experts) trip over.\n\n3) Implicit conversions are powerful\nBetween integer promotions, pointer comparisons with 0, macros like NULL, and varargs, the compiler has many opportunities to accept code that’s le