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 type int,
  • ((void *) 0), which has type void *, or
  • (1+5-6), which is an integer constant expression with value 0 and type int.

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.

Answer from Eric Postpischil on Stack Overflow
Top answer
1 of 2
14

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 type int,
  • ((void *) 0), which has type void *, or
  • (1+5-6), which is an integer constant expression with value 0 and type int.

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.

2 of 2
2

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) NULL which expands to an implementation-defined null pointer constant. C11dr §7.19 3

An 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);
🌐
CodeWithHarry
codewithharry.com › tutorial › c-null-pointer
NULL Pointer | C Tutorial | CodeWithHarry
NULL pointers are often used to initialize a pointer variable, where we wish to represent that the pointer variable isn’t currently assigned to any valid memory address yet. ... A NULL pointer generally points to a NULL or 0th memory location, so in simple words, no memory is allocated to a NULL pointer. The dereferencing behavior of a NULL pointer is very much similar to that of a void pointer. A NULL pointer itself is a kind of a VOID pointer and hence, we have to typecast it into any data type the way we do to a void pointer before dereferencing.
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
c - Are there reasons to assign NULL instead of 0 to non-pointer variables? - Software Engineering Stack Exchange
A common practice is to assign variables with 0 and pointers with NULL. int p = NULL; // instead of int p = 0; int *ptr = NULL; int &ref=p; Are there reasons to assign NULL instead of 0 to a non-pointer variable type? More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 23, 2016
Nullable: Value Types, Reference Types, and Compiler Behavior
C# doesnt has real nullable reference type. When you declare a class like Person? person you just tell compiler 'warn me if i use this variable without checking for null' other than that Person person and Person? person has no different in runtime, it just your compiler checking for you. For value type int? i actually syntax sugar for Nullable i which is real type and when you dont check it, it will throw exception. I dont know why C# team dont just expand Nullable type to support reference type, anyway it is what it is now More on reddit.com
🌐 r/csharp
16
6
August 12, 2025
how do I check if the type of data type is null?
You don't need to use typeof to check for null or undefined, you can just use strict equality operator '===' if (jsType === null) { ... } More on reddit.com
🌐 r/learnjavascript
9
3
October 16, 2023
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.

🌐
cppreference.com
en.cppreference.com › cpp › types › NULL
NULL - cppreference.com
January 3, 2025 - 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.
🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - Null is a constant built into C, C++, and C#. It has a value of zero. Null can also be the value of a pointer that points nowhere.
🌐
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 - Any pointer which is assigned the value NULL becomes a null pointer. ... Pointers are powerful programming tools that store the memory address of variables, etc., elements of a program.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › pointers › null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - Master Python while learning data structures, algorithms, and more! ... In C, the NULL pointer is defined in the <stddef.h> header file. It is typically assigned to a pointer to indicate that it is not pointing to any valid memory. #include <stddef.h> pointer_type *pointer_name = NULL;
🌐
Quora
quora.com › What-is-the-data-type-of-NULL-in-C-Is-it-long
What is the data type of NULL in C++, Is it 'long'? - Quora
Answer: No. Null is nothing like nothing. Let me explain. Let's assume, I bought pizza. I ate all the pieces and it's nothing in box. It is “Zero”. Now, suppose, I don't have pizza neither the box, I have “Null” . Nothing. No data with no type. Everything can be solved using Pizzas!
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Null-Pointers.html
Null Pointers (GNU C Language Manual)
The cleanest way to get a null pointer is by writing NULL, a standard macro defined in stddef.h. You can also do it by casting 0 to the desired pointer type, as in (char *) 0. (The cast operator performs explicit type conversion; See Explicit Type Conversion.) You can store a null pointer in any lvalue whose data type is a pointer type:
🌐
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 - If any pointer is being compared to 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 is typecast to (void *) is both a null pointer and a null ...
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - In the C programming language, a null pointer is a pointer that does not point to any memory location and hence does not hold the address of any variables. It just stores the segment's base address.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
January 10, 2025 - The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
A NULL pointer in C is a pointer that doesn't point to any of the memory locations. The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h. A pointer is initialized to NULL to avoid the unpredicted behavior of a
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.

🌐
The Valley of Code
thevalleyofcode.com › lesson › c-advanced › null
C Advanced: NULL values
C has NULL. NULL however is used differently from other languages. In C, NULL is limited to identifying a null pointer.
🌐
C For Dummies
c-for-dummies.com › blog
Zero and NULL and Pointers and Stuff | C For Dummies Blog
April 17, 2021 - The defined constant NULL represents a null or empty pointer. I’ve written before that the constant isn’t defined as zero, though such an assumption could lead you into trouble. Keep in mind that NULL is a defined constant. It’s declared in the stdio.h header file or, more likely, its ...
🌐
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
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 is), that ‘n...
🌐
Medium
medium.com › @shlomohassid › null-how-do-you-define-nothing-and-why-would-you-07683bdbe63a
NULL: How Do You Define Nothing? And Why Would You? | by Momi | Medium
May 31, 2025 - You can’t have a truly “null integer” or “null float” in C – you’d use 0 or some sentinel value instead. Likewise, some modern type-safe languages choose to avoid NULL entirely for regular references, offering different patterns to represent optional data.