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
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
Which C standard header file defines the symbolic constant for the NULL character (\0), which is the character used as string terminator? While I have defined it myself in the program as: #define More on stackoverflow.com
🌐 stackoverflow.com
c - Which windows.h header defines NULL? - Stack Overflow
I have to write some Windows code, and I want to separate it from the Linux part as much as possible. At one point I need to check for NULL in the Windows code, but I don't want to include stdio.h or More on stackoverflow.com
🌐 stackoverflow.com
"In which header file NULL macro is defined ? "
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
1 month ago
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › null-crt
NULL (CRT) | Microsoft Learn
October 26, 2022 - Feedback · Summarize this article ... 0. NULL is defined in the following header files: CRTDBG.H, LOCALE.H, STDDEF.H, STDIO.H, STDLIB.H, STRING.H, TCHAR.H, TIME.H and WCHAR.H....
🌐
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).
🌐
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
🌐
cppreference.com
en.cppreference.com › w › c › types › NULL.html
NULL - cppreference.com
January 12, 2024 - #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
🌐
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.

🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › null.3const.html
NULL(3const) - Linux manual page
Instead, always use NULL. NULL shouldn't be confused with NUL, which is an ascii(7) character, represented in C as '\0'.
🌐
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.
🌐
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].
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 92384-null-not-defined.html
Null Not Defined
Hi, Sometimes when I use NULL the compiler spits out an error saying NULL is not defined. Could anyone tell me the reason (in general) why this would happen. Cheers Alex ... Because you haven't #include'd one of the header files (eg <stdlib.h> or <cstdlib>) that #define's NULL.
🌐
Clc-wiki
clc-wiki.net › wiki › Talk:C_standard_library:string.h:NULL
Talk:C standard library:string.h:NULL - clc-wiki
If none of the standard headers defining NULL are included, it's fine to (re)define it; otherwise it belongs to the implementation and redefining it invokes undefined behaviour (this might cause problems if e.g. a compiler relies on a header file definition of NULL equating to __null and then uses internal magic to translate that into 0 or (void *)0).
🌐
Scribd
scribd.com › document › 856860870 › Header-Files-and-Macros
Defining NULL in C Header Files | PDF | Macro (Computer Science) | Software
Header Files and Macros - Free download as PDF File (.pdf), Text File (.txt) or read online for free. The document provides a detailed overview of the C preprocessor, including its uses for directives, constants, macros, and conditional compilation. It explains how to include header files, define constants, and create macros, while also addressing common issues such as avoiding multiple inclusions and handling side effects in macros.
🌐
Code with C
codewithc.com › code with c › blog › understanding the null macro: exploring header files in c++
Understanding The Null Macro: Exploring Header Files In C++ - Code With C
February 12, 2024 - Value of ptr (expected to be 0): 0 Is ptr NULL? Yes Is ptr 0? Yes Cannot dereference NULL pointer. ... Start by including the <iostream> header for input-output stream operations.