The type of NULL may be either an integer type or void *. This is because the C standard allows it to be defined as either an integer constant expression or the result of a cast to void *.
C 2018 7.19 3 says NULL “expands to an implementation-defined null pointer constant” (when any of several headers have been included: <locale.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, or <wchar.h>).
C 6.3.2.3 3 says a null pointer constant is “An integer constant expression with the value 0, or such an expression cast to a type void *.”
Thus, a C implementation may define NULL as, for example:
0, which has typeint,((void *) 0), which has typevoid *, or(1+5-6), which is an integer constant expression with value 0 and typeint.
Even though NULL may have an integer type, it may be compared to and assigned to pointers, as in if (p == NULL) …. The rules for these operations say that an integer constant zero will be converted to the appropriate pointer type for the operation.
Although NULL may be defined to be 0, it is intended to be used for pointers, not as an integer zero. Programs should avoid doing that, and C implementations are generally better off defining it as ((void *) 0) to help avoid mistakes where it might be accepted as an integer value.
In most C implementations, converting NULL to an integer will yield zero. However, this is not guaranteed in the C standard. It is allowed that (int) NULL or (uintptr_t) NULL will produce the address of some special “do not use” location rather than zero. Even (int) (void *) 0 might produce such an address rather than zero.
When an integer constant zero is converted to a pointer type, it is treated specially by the C implementation; it produces a null pointer for that implementation even if its null pointer uses an address other than zero. The fact that it is an integer constant means the compiler can apply this special treatment where it recognizes the conversion in the source code. If we have some non-constant expression, such as an int variable x, then (void *) x is not guaranteed to yield a null pointer even if the value of x is zero.
The type of NULL may be either an integer type or void *. This is because the C standard allows it to be defined as either an integer constant expression or the result of a cast to void *.
C 2018 7.19 3 says NULL “expands to an implementation-defined null pointer constant” (when any of several headers have been included: <locale.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, or <wchar.h>).
C 6.3.2.3 3 says a null pointer constant is “An integer constant expression with the value 0, or such an expression cast to a type void *.”
Thus, a C implementation may define NULL as, for example:
0, which has typeint,((void *) 0), which has typevoid *, or(1+5-6), which is an integer constant expression with value 0 and typeint.
Even though NULL may have an integer type, it may be compared to and assigned to pointers, as in if (p == NULL) …. The rules for these operations say that an integer constant zero will be converted to the appropriate pointer type for the operation.
Although NULL may be defined to be 0, it is intended to be used for pointers, not as an integer zero. Programs should avoid doing that, and C implementations are generally better off defining it as ((void *) 0) to help avoid mistakes where it might be accepted as an integer value.
In most C implementations, converting NULL to an integer will yield zero. However, this is not guaranteed in the C standard. It is allowed that (int) NULL or (uintptr_t) NULL will produce the address of some special “do not use” location rather than zero. Even (int) (void *) 0 might produce such an address rather than zero.
When an integer constant zero is converted to a pointer type, it is treated specially by the C implementation; it produces a null pointer for that implementation even if its null pointer uses an address other than zero. The fact that it is an integer constant means the compiler can apply this special treatment where it recognizes the conversion in the source code. If we have some non-constant expression, such as an int variable x, then (void *) x is not guaranteed to yield a null pointer even if the value of x is zero.
What type is NULL?
Short answer: The type of NULL is void* or int, long, unsigned, ...
Addition to @Eric Postpischil fine answer to point out more coding pitfalls.
(macro)
NULLwhich expands to an implementation-defined null pointer constant. C11dr §7.19 3An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant. §6.3.2.3 3
The type of NULL has many possibilities given "integer constant". Portable code should not assume a particular type. NULL is best used in pointer contexts.
// Poor code as NULL may not match the type of the specifier - undefined behavior
printf("%p\n", NULL); // poor
printf("%d\n", NULL); // poor
// Better
printf("%d\n", (int) NULL);
// Best. NULL is best used in a pointer context, print as a pointer
printf("%p\n", (void *) NULL);
Understanding difference between 0 and NULL
c - Are there reasons to assign NULL instead of 0 to non-pointer variables? - Software Engineering Stack Exchange
Nullable: Value Types, Reference Types, and Compiler Behavior
how do I check if the type of data type is null?
Videos
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:
CopyNULLwhich 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.
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,
NULLand0may be used interchangeably as null pointer constants. 0will function as an integer (non-pointer) constant, butNULLmight not, depending on how the C implementation defines it.- For the purpose of detecting errors,
NULLand0do not work exactly the same; usingNULLwith a good definition serves to help detect some mistakes that using0does 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.
0 being an int like other integers, sizeof(0) will yield 4 bytes.
sizeof(NULL) will yield 8 bytes. In binary system, it is 8x8=64 bits, all bits with 0.
Pointers have 8 bytes allocated against characters with 1 bytes and integers 4 bytes. Is 8 bytes the maximum bytes for any datatype? I believe so as NULL is set to 8 bytes apparently for that reason to take care NULL denotes 0 for all datatypes.
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.
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.