NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.

In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).

Otherwise, add

#include <stddef.h>

to get the NULL definition.

Answer from unwind on Stack Overflow
🌐
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 - The above code will show an error as "NULL undeclared Error". The reason for the NULL undeclared error is "NULL" is not a built-in constant. Why do we need NULL? When we create some pointer in our program, they are used for storing addresses.
Discussions

'NULL' undeclared (first use in this function)
The first attempt is in: But when creating xTaskCreatePinnedToCore() in this code I get an error: ‘NULL’ undeclared (first use in this function) #include #include #include // #include void createAllTasks() { // All xTaskCreate… goes here and utilize the macros created // in taskConfig.h ... More on community.platformio.org
🌐 community.platformio.org
0
0
May 13, 2021
c - NUL undeclared- first use in this function - Stack Overflow
From the web I understand that C contains NUL keyword. But while compiling I get an error NUL undeclared first use in this function' My code fragment: for (;;) { char ch = (*pzDest = *pz... More on stackoverflow.com
🌐 stackoverflow.com
October 24, 2016
'NULL' undeclared...
Allegro.cc Forums » Programming Questions » 'NULL' undeclared... More on allegro.cc
🌐 allegro.cc
error C2065: Undeclared Identifier
I'm using Visual Studio 2005 on Windows XP. I'm trying to compile a C program. I'm down to one error. When I try to compile the following, I get the error: initmol.c(20) : error C2065: 'NULL' : undeclared identifier Here's the code. It errors out on the IF statement: #include #include... More on tek-tips.com
🌐 tek-tips.com
5
0
July 21, 2006
🌐
TechOverflow
techoverflow.net › 2019 › 06 › 20 › how-to-fix-c-error-null-undeclared
How to fix C error 'NULL undeclared' | TechOverflow
April 1, 2026 - main.c: In function ‘main’: main.c:3:17: error: ‘NULL’ undeclared (first use in this function) void* ptr = NULL; ^~~~ main.c:3:17: note: each undeclared identifier is reported only once for each function it appears in
🌐
PlatformIO Community
community.platformio.org › t › null-undeclared-first-use-in-this-function › 21417
'NULL' undeclared (first use in this function) - PlatformIO Community
May 13, 2021 - I try to get rid of 900 lines of code in a main.cpp file. For that I tried to set up some kind of project directory structure. The first attempt is in: But when creating xTaskCreatePinnedToCore() in this code I get an error: ‘NULL’ undeclared (first use in this function) #include #include #include // #include void createAllTasks() { // All xTaskCreate… goes here and utilize the macros created // in taskConfig.h // Be...
🌐
Depot Games
allegro.cc › forums › thread › 332697
'NULL' undeclared...
Allegro.cc Forums » Programming Questions » 'NULL' undeclared...
🌐
Sabe
sabe.io › blog › c-null
A Guide to NULL in C - Sabe.io
February 15, 2022 - When you want to initialize a pointer but don't yet have a value, you can use NULL. ... To ensure that you don't get the use of undeclared identifier error, make sure to include the stdio.h header file that comes with C.
Find elsewhere
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › development tools › microsoft: visual c++
error C2065: Undeclared Identifier | Tek-Tips
July 21, 2006 - I ended up replacing malloc.h with stdio.h and got rid of the malloc cast and it solved the problem: ... #include <stdio.h> #include "molecule.h" MOLECULE *InitMol( void ) { MOLECULE *m; if ( (m = (MOLECULE *)sizeof( *m ) ) == NULL ) { return( NULL ); } m->N_Hydro = -1; m->N_Atoms = 0; m->N_Bonds = 0; m->N_Comps = 0; return( m ); }
🌐
GameDev.net
gamedev.net › forums › topic › 207884-what-the-heck----null-undeclared
What the heck -- NULL undeclared? - General and Gameplay Programming - GameDev.net
June 18, 2017 - Um, no? NULL is defined in stdlib.h/cstdlib and is very much standard C and C++. Usually NULL being unrecognized means either (1) some VERY old legacy code has been screwing with the preprocessor, or (2) The compiler is very, very confused.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 103806-null-undeclared-first-use-function.html
'NULL' undeclared (first use this function)
June 3, 2008 - My Website "Circular logic is good because it is." ... NULL is defined in <cstdlib> or <cstddef>. You need to include them in the file. Tough, since this is C++ I recommend using 0 instead of null.
🌐
PlatformIO Community
community.platformio.org › t › null-undeclared-first-use-in-this-function › 21417 › 2
'NULL' undeclared (first use in this function) - #2 by maxgerhardt - PlatformIO Community
May 13, 2021 - NULL is a macro defined in stdlib.h. Though this project seems to be a weird mixing of C and C++ files without extern "C" declarations, so linking will fail… Are you sure you don’t want to just write C++ directly and use the builtin nullptr?
🌐
EmbDev
embdev.net › arm programming with gcc/gnu tools
error: 'NULL' and 'stderr' undeclared - EmbDev.net
June 23, 2017 - EmbDev.net · Contact – Data privacy information – Advertising on EmbDev.net
🌐
Code::Blocks
forums.codeblocks.org › index.php
NULL undefined
Code::Blocks Forums » · User forums » · Help » · NULL undefined · « previous next » · Send this topic · Print · Pages: [1] 2 3 All Go Down · Send this topic · Print · Pages: [1] 2 3 All Go Up · « previous next » · Code::Blocks Forums » · User forums » ·
🌐
GitHub
github.com › microsoft › vscode-cpptools › issues › 2607
identifier "NULL" is undefined · Issue #2607 · microsoft/vscode-cpptools
October 4, 2018 - Issue Type: Bug I do not know from when on anytime I type NULL the reporter says this. Everything seemed to be okay before. My c_cpp_properties.json is pasted here, if required. { "configurati...
Author   microsoft
Top answer
1 of 4
4

C 2011 Standard, online draft

6.3.2.3 Pointers
...
3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) 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.
66) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.

The macro NULL is always defined as a zero-valued constant expression; it can be a naked 0, or 0 cast to void *, or some other integral expression that evaluates to 0. As far as your source code is concerned, NULL will always evaluate to 0.

Once the code has been translated, any occurrence of the null pointer constant (0, NULL, etc.) will be replaced with whatever the underlying architecture uses for a null pointer, which may or may not be 0-valued.

2 of 4
4

WhozCraig wrote these comments to a now-deleted answer, but it could be promoted to a full answer (and that's what I've done here). He notes:

Interesting note: AS/400 is a very unique platform where any non-valid pointer is considered equivalent to NULL. The mechanics which they employ to do this are simply amazing. "Valid" in this sense is any 128-bit pointer (the platform uses a 128bit linear address space for everything) containing a "value" obtained by a known-trusted instruction set. Hard as it is to believe, int *p = (int *)1; if (p) { printf("foo"); } will not print "foo" on that platform. The value assigned to p is not source-trusted, and thus, considered "invalid" and thereby equivalent to NULL.

It's frankly startling how it works. Each 16-byte paragraph in the mapped virtual address space of a process has a corresponding "bit" in a process-wide bitmap. All pointers must reside on one of these paragraph boundaries. If the bit is "lit", the corresponding pointer was stored from a trusted source, otherwise it is invalid and equivalent to NULL. Calls to malloc, pointer math, etc, are all scrutinized in determining whether that bit gets lit or not. And as you can imagine, putting pointers in structures brings a whole new world of hurt on the idea of structure packing.


This is marked community-wiki (it's not my answer — I shouldn't get the credit) but it can be deleted if WhozCraig writes his own answer.

What this shows is that there are real platforms with interesting pointer properties.

There have been platforms where #define NULL ((void *)0) is not the usual definition; on some platforms it can be just 0, on others, 0L or 0ULL or other appropriate values as long as the compiler understands it. C++ does not like ((void *)0) as a definition; systems where the headers interwork with C++ may well not use the void pointer version.

I learned C on a machine where the representation for the char * address for a given memory location was different from the int * address for the same memory location. This was in the days before void *, but it meant that you had to have malloc() properly declared (char *malloc(); — no prototypes either), and you had to explicitly cast the return value to the correct type or you got core dumps. Be grateful for the C standard (though the machine in question, an ICL Perq — badged hardware from Three Rivers — was largely superseded by the time the standard was defined).

🌐
LabEx
labex.io › tutorials › c-how-to-fix-undeclared-identifier-in-c-419180
How to fix undeclared identifier in C | LabEx
In C, an identifier is simply a name that refers to something in your program, such as: ... An identifier is "undeclared" when you try to use it without first telling the compiler what it is.