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
Old compilers and NULL
NULL is a macro that should be defined in a header . In theory it's possible that some compiler ships without a definition for it (can anyone comment if there's a C standard where this is permitted?), in practice you've simply forgotten to include the right header. More on reddit.com
🌐 r/C_Programming
31
36
March 29, 2022
🌐
cppreference.com
en.cppreference.com › c › types › NULL
NULL - cppreference.com
A null pointer constant may be converted to any pointer type; such conversion results in the null pointer value of that type. ... POSIX requires NULL to be defined as an integer constant expression with the value 0 cast to void*.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_null.htm
C library - NULL Macro
NULL 0L: A long integer literal representing a null pointer. Following is the C library syntax of the NULL Macro. #define NULL ((char *)0) or, #define NULL 0L or #define NULL 0
🌐
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....
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › null.3const.html
NULL(3const) - Linux manual page
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>. It is undefined behavior to dereference a null pointer, and that usually causes a segmentation fault in practice.
🌐
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
Find elsewhere
🌐
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.
🌐
Medium
medium.com › @pauljlucas › nullptr-in-c23-571782008dad
nullptr in C23. The new nullptr keyword in C23. | by Paul J. Lucas | Medium
July 8, 2025 - To fix these problems, C23 borrowed the nullptr keyword from C++ that’s a literal for the null pointer. Like void* and 0, nullptr implicitly converts to any type of pointer; unlike NULL, it’s guaranteed to be a pointer.
🌐
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).
🌐
IndiaBIX
indiabix.com › c-programming › pointers › discussion-278
Pointers General Questions - C Programming Questions and Answers Discussion Page For Q.3
... I need the purpose of using wchar.h in this program. ... Macro is available in stdlib.h, stddef.h, locale.h. etc., We know that NULL represents nothing. But it should have some value. While using NULL in program we may get garbage value. So to avoid that garbage values. NULL macro defines ...
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - Normally, the null pointer does not point to anything. NULL is a macro constant defined in several header files in the C programming language, including stdio.h, alloc.h, mem.h, stddef.h, and stdlib.h.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 92384-null-not-defined.html
Null Not Defined
There is no guarantee nor any requirement that any of the standard headers #include <cstddef>. However, in practice, the headers with some compilers do. ... However, since NULL in C++ is defined as 0, you might start using 0 instead of NULL so as not to depend on whether a macro is defined or not.
🌐
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].
🌐
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).
🌐
Clc-wiki
clc-wiki.net › wiki › C_standard_library:string.h:NULL
C standard library:string.h:NULL - clc-wiki
The NULL macro is defined by many headers to expand to an implementation-defined null pointer constant. The advantage over using literal 0 is the warnings produced by current compilers on most forms of improper use. It might simply be defined as: ... Defined in <stddef.h>, <locale.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>.
🌐
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.