The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

The C++ standard requires that NULL be defined in the c* header corresponding to each of those.

The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.

The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.

From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).

Answer from Jerry Coffin 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.
Published   January 10, 2025
🌐
C-faq
c-faq.com › null › macro.html
Question 5.4
A: As a matter of style, many programmers prefer not to have unadorned 0's scattered through their programs, some representing numbers and some representing pointers. Therefore, the preprocessor macro NULL is defined (by several headers, including <stdio.h> and <stddef.h>) as a null pointer constant, typically 0 or ((void *)0) (see also question 5.6).
Discussions

I'm writing a simple header that handles malloc returning a NULL pointer, is that good practice?
Sure, you could do that. Two caveats though: Trying to allocate multiple times doesn't seem like a good idea, in general. If the OS is running out of memory, usually the best thing you can do is just to fail fast and die. Prevent the program from get entangled further into a hard to assess state, while giving your OS a chance to survive and get its shit together. Any non-trivial C program tends to have its own logging facility and error handling mechanism, which is why it's generally sortof frowned upon when libraries directly print to stdout/stderr or call exit(). Your use case could be argued to be an exception though. More on reddit.com
🌐 r/C_Programming
23
13
December 23, 2021
Which C standard header file defines NULL character? - Stack Overflow
Additionally, assuming it's defined ... the header file just for the purpose of accessing this character against defining it myself? ... Note that the null byte '\0' (which is actually an integer constant in C, in contrast to C++) is separate from the standard name for the null pointer constant ... More on stackoverflow.com
🌐 stackoverflow.com
&quot;In which header file NULL macro is defined ?&nbsp;&quot;
NULL is a macro representing a null pointer constant. It is defined as #define NULL ((void*)0) in C. ... Pre-processor: Though macros are handled by the pre-processor, NULL is defined in header files, not directly by the pre-processor. More on testbook.com
🌐 testbook.com
1
1707
May 13, 2026
Old compilers and NULL
C++11 introduced nullptr (IIRC C23 will have something equivalent), which unlike NULL++98, does have pointer type, namely std::nullptr_t ≡ decltype(nullptr), whose only permissible value is nullptr (lamentably, there is no undef_t sth std::nullptr_t ≡ undef_t *). More on reddit.com
🌐 r/C_Programming
31
36
March 29, 2022
🌐
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
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › null.3const.html
NULL(3const) - Linux manual page
NULL represents a null pointer constant, that is, a pointer that does not point to anything. C11, POSIX.1-2024. C89, POSIX.1-1988. The following headers also provide NULL: <locale.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <unistd.h>, and <wchar.h>.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › null-crt
NULL (CRT) | Microsoft Learn
October 26, 2022 - You can try changing directories. Feedback · Summarize this article for me · NULL is the null-pointer value used with many pointer operations and functions. It's equivalent to 0. NULL is defined in the following header files: CRTDBG.H, LOCALE.H, ...
🌐
cppreference.com
en.cppreference.com › w › c › types › NULL.html
NULL - cppreference.com
January 12, 2024 - POSIX requires NULL to be defined as an integer constant expression with the value ​0​ cast to void*. ... #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); }
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_null.htm
C library - NULL Macro
The C library NULL Macro represent the value of a null pointer constant that may be defined as ((void*)0), 0 or 0L depending on the compiler vendor. Following is the list of NULL macro structure − Following is the C library syntax of the NULL ...
🌐
Reddit
reddit.com › r/c_programming › i'm writing a simple header that handles malloc returning a null pointer, is that good practice?
r/C_Programming on Reddit: I'm writing a simple header that handles malloc returning a NULL pointer, is that good practice?
December 23, 2021 -

Hello. I'm a beginner and I'm doing the Advent of Code 2021 to develop my skills. I'm trying to use more malloc and realloc, because I think that's important. I just found out that these functions might return a NULL pointer in case they can't allocate memory, and that you're supposed to check whether the allocation worked or not.

I think having to do that in every instance I use any allocation function would be cumbersome, so I tried writing a header to use in all my Advent of Code programs that handles all of that for me. It tries to allocate memory CHANCES times, and if it fails, it exits the program. However, I wanted to ask whether that's a good practice to have for my future programs. Here's the header:

#include <stdio.h>
#include <stdlib.h>

#define CHANCES 5

static void allocError(void)
{
	printf("allocation error: couldn't allocate more memory\n");
	exit(1);
}

static void *altAlloc(void *pointer, size_t size)
{
	void *temp;
	int failchance = 0;

	while (failchance++ < CHANCES) {
		temp = realloc(pointer, size);
		if (!temp)
			continue;
		pointer = temp;
		return pointer;
	}
	allocError();
}

It seems to be working perfectly when it's able to allocate the memory, but I don't know if it'd properly exit the program in case it's not. I think it is, though, since the code isn't that complicated.

🌐
Codecademy
codecademy.com › docs › pointers › null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - In C, the NULL pointer is defined in the <stddef.h> header file.
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - It just stores the segment's base address. That is, the null pointer in C holds the value Null, but the type of the pointer is void. A null pointer is a special reserved value declared in a header file called stddef.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › null-undeclared-error-in-c-c-and-how-to-resolve-it
NULL undeclared error in C/C++ and how to resolve it - GeeksforGeeks
July 15, 2025 - Assign 0: Instead of assigning NULL to num we can simply assign 0 which indicate that it is not pointing to any address, so the simplest solution is simply assigning 0. Below code shows its implementation: ... Include "stddef.h" Header file: In stddef.h header file NULL is already defined, so we can include this header file in our program and our program will compile and execute without any error.
🌐
Javatpoint
javatpoint.com › null-pointer-in-c
Null Pointer in C - javatpoint
Null Pointer in C with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.
🌐
Reddit
reddit.com › r/c_programming › old compilers and null
r/C_Programming on Reddit: Old compilers and NULL
March 29, 2022 - C++11 introduced nullptr (IIRC C23 will have something equivalent), which unlike NULL++98, does have pointer type, namely std::nullptr_t ≡ decltype(nullptr), whose only permissible value is nullptr (lamentably, there is no undef_t sth std::nullptr_t ≡ undef_t *).
🌐
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 ...
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stddef.h
C Programming/stddef.h - Wikibooks, open books for an open world
November 1, 2007 - stddef.h is a header file in the standard library of the C programming language that defines the macros NULL and offsetof as well as the types ptrdiff_t, wchar_t, and size_t[1].
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... NULL is a special value that represents a "null pointer" - a pointer that does not point to anything.