They might be, they might not be.

Both C and C++ define NULL, but in slightly different ways.

null is not part of either standard; it is neither a keyword nor a reserved word, so you can use it as a variable name, class name &c..

The preferred way of denoting pointer null-ness in C++ is nullptr which has type especially designed for pointer nullness.

Answer from Bathsheba on Stack Overflow
Top answer
1 of 3
15

They might be, they might not be.

Both C and C++ define NULL, but in slightly different ways.

null is not part of either standard; it is neither a keyword nor a reserved word, so you can use it as a variable name, class name &c..

The preferred way of denoting pointer null-ness in C++ is nullptr which has type especially designed for pointer nullness.

2 of 3
2

C standard says :

6.3.2.3/3 Pointers

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

stddef.h then contains the NULL-define to that value.

C++ (another language, roughly related to C, certainly not in the way people usually think) used NULL in its very early versions (don't use it!). But in pre-C++11 releases, the constant 0 was defined as the way to represent pointers to nothing. Alas, this has some serious drawbacks and then C++11 defined the nullptr constant. Note that nullptr is a keyword.

C++ standard says:

2.14.17/1 Pointer literals

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.]

3.9.1/10 Fundamental types

A value of type std::nullptr_t is a null pointer constant. Such values participate in the pointer and the pointer to member conversions. sizeof(std::nullptr_t) shall be equal to sizeof(void*).

About NULL in C++ standard says:

18.2/3 Types

The macro NULL is an implementation-defined C++ null pointer constant.

Top answer
1 of 11
443

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.

🌐
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 - Below statement checks if the string pointer is pointing at a not-null character. if (*string_pointer) In C language string is nothing but an array of char type. It stores each of the characters in a memory space of 1 byte. Each array is terminated with '\0' or null character but if we store a '0' inside a string both are not same according to the C language.
🌐
Embedded Artistry
embeddedartistry.com › home › blog › migrating from c to c++: null vs nullptr
Migrating from C to C++: NULL vs nullptr - Embedded Artistry
December 15, 2021 - The general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past. What’s so wrong with our old friend NULL, anyway? Traditionally, the NULL macro is an implementation defined constant representing a null pointer, usually the integer 0.
🌐
Post.Byes
post.bytes.com › home › forum › topic
null and NULL: is there any difference? - Post.Byes
... Re: null and NULL: is there any difference? On Wed, 24 Sep 2003, RHNewBie wrote: [color=blue] > Hello, > What is the difference between null and NULL. Are x == null and x == > NULL the same? The compiler is gcc 3.2.[/color] C language has a NULL but there is no null.
🌐
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...
Find elsewhere
🌐
C For Dummies
c-for-dummies.com › blog
The Difference Between NULL and Zero | C For Dummies Blog
Even when all this still confuses you, just drive it into your head: NULL isn’t used as a placeholder for zero. In the C language, which features a memory location variable type (the pointer), it’s necessary to have NULL to represent an empty memory location.
🌐
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.
🌐
Quora
quora.com › What-is-difference-between-NUL-and-NULL-in-programming
What is difference between NUL and NULL in programming? - Quora
Basically, NULL in case of C Programming can be used in the following ways: [code]null macro null pointer null string (\0) null constant [/code]For Example; When a byte is set to zero, called null character....
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.

🌐
Cppreference
en.cppreference.com › w › cpp › types › NULL.html
NULL - cppreference.com
The macro NULL is an implementation-defined null pointer constant. In C, the macro NULL may have the type void*, but that is not allowed in C++ because null pointer constants cannot have that type.
🌐
C For Dummies
c-for-dummies.com › blog
Zero and NULL and Pointers and Stuff | C For Dummies Blog
Finally, NULL isn’t memory location (address) zero. It may be the value zero as it’s defined, but it’s not a memory location. 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 ...
🌐
Handmade Network
hero.handmade.network › forums › code-discussion › t › 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
You swapped c and c++?, in that case I get it. ratchetfreak Why C++ doesn't define NULL as nullptr is because idiot programmers used NULL as the 0 constant, most probably to avoid "magic number" warnings and nullptr doesn't convert to int implicitly so a #define NULL nullptr would break the C compatibility.
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.

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:

NULL

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.

🌐
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...
🌐
Wikipedia
en.wikipedia.org › wiki › Null_pointer
Null pointer - Wikipedia
3 weeks ago - Prior to C23, the preprocessor macro NULL was provided, defined as an implementation-defined null pointer constant in <stdlib.h>, which in C99 can be portably expressed with #define NULL ((void*)0), the integer value 0 converted to the type void* (see pointer to void type).