nullptr is C++ only; it is not needed in C because in C ((void*)0) is convertible to any other pointer type without casts.

If you really really really like to type nullptr in C, you can use

#define nullptr ((void*)0)

and it would then work mostly the same.


Notice that C has the NULL macro from <stddef.h>; it is readable too, but its expansion is implementation-defined, so it might be either ((void*)0) or 0 (or something really strange); if it expands to 0, you wouldn't get any diagnostics from

int a = NULL;
Answer from Antti Haapala -- Слава Україні on Stack Overflow
Top answer
1 of 3
14

The error you are getting is because the compiler doesn't recognize the nullptr keyword. This is because nullptr was introduced in a later version of visual studio than the one you are using.

There's 2 ways you might go about getting this to work in an older version. One idea comes from Scott Meyers c++ book where he suggests creating a header with a class that emulates nullptr like this:

Copyconst // It is a const object...
class nullptr_t 
{
  public:
    template<class T>
    inline operator T*() const // convertible to any type of null non-member pointer...
    { return 0; }

    template<class C, class T>
    inline operator T C::*() const   // or any type of null member pointer...
    { return 0; }

  private:
    void operator&() const;  // Can't take address of nullptr

} nullptr = {};

This way you just need to conditionally include the file based on the version of msvc

Copy#if _MSC_VER < 1600 //MSVC version <8
     #include "nullptr_emulation.h"
#endif

This has the advantage of using the same keyword and makes upgrading to a new compiler a fair bit easier (and please do upgrade if you can). If you now compile with a newer compiler then your custom code doesn't get used at all and you are only using the c++ language, I feel as though this is important going forward.

If you don't want to take that approach you could go with something that emulates the old C style approach (#define NULL ((void *)0)) where you make a macro for NULL like this:

Copy#define NULL 0

if(data == NULL){
}

Note that this isn't quite the same as NULL as found in C, for more discussion on that see this question: Why are NULL pointers defined differently in C and C++?

The downsides to this is that you have to change the source code and it is not typesafe like nullptr. So use this with caution, it can introduce some subtle bugs if you aren't careful and it was these subtle bugs that motivated the development of nullptr in the first place.

2 of 3
7

nullptr is part of C++11, in C++03 you simply use 0:

Copyif (!Data)
Discussions

c++ - Why is NULL undeclared? - Stack Overflow
It's defined as ((void *)0) by most C standard library implementations. 2013-09-16T10:55:42.887Z+00:00 ... This is the best short answer (and technically precise) I've ever read regarding the topic: NULL vs. 0 vs. nullptr. More on stackoverflow.com
🌐 stackoverflow.com
[coap] use of `nullptr` in C API
I just fetched the most recent Zephyr zephyr-v2.5.0-148-g4ee876b797b1 which just merged #4247 (CoAP block transfers). Build fails... (board is nrf52)...both crosstools arm compiler and arm gcc. bui... More on github.com
🌐 github.com
2
February 15, 2021
g++ - C++ Error 'nullptr was not declared in this scope' in Eclipse IDE - Stack Overflow
I am running Eclipse Helios and I have g++-4.6 installed. Hope I am not wrong that g++4.6 implements C++ 11 features. I have created a C++ project which uses the nullptr and auto keywords. The build More on stackoverflow.com
🌐 stackoverflow.com
"use of undeclared identifier nullptr" everywhere 'nullptr' is used
Windows 7 SP1 VSCode 1.22.1 vscode-cpptools 0.16.1 Other extensions: "C/C++ Clang Command Adapter", "C++ Intellisense"; issue persist after disabling those extensions How to rep... More on github.com
🌐 github.com
2
April 28, 2018
🌐
GNU
gcc.gnu.org › pipermail › gcc-patches › 2022-August › 600257.html
[PATCH v2] c: Implement C23 nullptr (N3042)
*/ +/* { dg-do compile } */ +/* { dg-options "-std=c17 -pedantic-errors" } */ + +int * +fn (int *p) +{ + p = nullptr; /* { dg-error "'nullptr' undeclared" } */ + return p; +} diff --git a/gcc/testsuite/gcc.dg/c2x-nullptr-1.c b/gcc/testsuite/gcc.dg/c2x-nullptr-1.c new file mode 100644 index 00000000000..ca01b3e3296 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-nullptr-1.c @@ -0,0 +1,259 @@ +/* Test valid usage of C23 nullptr.
🌐
GitHub
github.com › openthread › openthread › issues › 6171
[coap] use of `nullptr` in C API · Issue #6171 · openthread/openthread
February 15, 2021 - build/ZEPHYR/modules/lib/openthread/include/openthread/coap.h: In function 'otCoapSendRequestBlockWise': build/ZEPHYR/modules/lib/openthread/include/openthread/coap.h:946:108: error: 'nullptr' undeclared (first use in this function) 946 | return otCoapSendRequestBlockWiseWithParameters(aInstance, aMessage, aMessageInfo, aHandler, aContext, nullptr, | ^~~~~~~ build/ZEPHYR/modules/lib/openthread/include/openthread/coap.h:946:108: note: each undeclared identifier is reported only once for each function it appears in build/ZEPHYR/modules/lib/openthread/include/openthread/coap.h: In function 'otCoapSendResponseBlockWise': build/ZEPHYR/modules/lib/openthread/include/openthread/coap.h:1107:89: error: 'nullptr' undeclared (first use in this function) 1107 | return otCoapSendResponseBlockWiseWithParameters(aInstance, aMessage, aMessageInfo, nullptr, aContext, | ^~~~~~~ jwhui ·
Author   openthread
🌐
GitHub
github.com › Microsoft › vscode-cpptools › issues › 1900
"use of undeclared identifier nullptr" everywhere 'nullptr' is used · Issue #1900 · microsoft/vscode-cpptools
April 28, 2018 - Windows 7 SP1 VSCode 1.22.1 vscode-cpptools 0.16.1 Other extensions: "C/C++ Clang Command Adapter", "C++ Intellisense"; issue persist after disabling those extensions How to reproduce: Create pointer Assign or check if created pointer is...
Author   microsoft
🌐
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
Find elsewhere
🌐
Educative
educative.io › answers › what-is-the-null-undeclared-error-in-c-cpp
What is the NULL undeclared error in C/C++?
Executing the above code gives the NULL undeclared error at main.cpp:5:12. This happens because NULL is not a built-in constant in the C or C++ languages. In fact, in C++, it’s more or less obsolete, instead, just a plain ...
🌐
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 - #define NULL 0: Using #define NULL 0 line in our program, we can solve the NULL undeclared error. Below code shows its implementation: ... In newer C++(C++11 and higher):: nullptr is a built-in constant, so we can use it instead of using NULL.
🌐
Cplusplus
cplusplus.com › forum › general › 131711
'nullptr' was not declared in this scope - C++ Forum
#include <iostream> using namespace std; class Person { private: string name; public: Person(){} Person(string theName) { name = theName; } Person(Person& other) { if (other != nullptr) name = other.name; } };
🌐
Reddit
reddit.com › r/learnprogramming › first time working with generics in c++, running into all kinds of compile errors.
r/learnprogramming on Reddit: First time working with generics in C++, running into all kinds of compile errors.
March 12, 2017 -

I'm making a basic stack class. But I haven't used generics before, and I'm seeing all sorts of weird errors when I try to compile.

Stack.h:

//My name

#include "StackNode.h"

template <class T> class Stack {
	public:
		void push(T const&);
		T pop();
		T peek() const;
		void clear();
		bool isEmpty() const;
	private:
		StackNode<T>* top;
		void clearHelper(StackNode<T>* start);
};

Stack.cpp:

//My name

#include "Stack.h"

template <class T> void Stack<T>::push(T const& val) {
	StackNode<T>* newNode(val, top);
	top = newNode;
}

template <class T> T Stack<T>::pop() {
	if(top == nullptr) {
		return nullptr;
	}
	T tempaT = top.getValue();
	StackNode<T>* tempNode = top;
	top = tempNode.getPrev();
	delete tempNode;
	return tempaT;
}

template <class T> T Stack<T>::peek() const {
	if(top == nullptr) {
		return nullptr;
	} else {
		return top.getValue();
	}
}

void Stack<T>::clear() {
	if(top == nullptr) {return;}
	clearHelper(top);
	top = nullptr;
}

void Stack<T>::clearHelper(StackNode<T>* start) {
	if(start == nullptr) {
		return;
	} else {
		clearHelper(start.getPrev());
		delete start;
	}
}

bool Stack<T>::isEmpty() const {
	return top == nullptr;
}

The errors are as follows. I can't even find other people with the nullptr error.

Stack.cpp:11:12: error: use of undeclared identifier 'nullptr'
        if(top == nullptr) {
                  ^
Stack.cpp:12:10: error: use of undeclared identifier 'nullptr'
                return nullptr;
                       ^
Stack.cpp:14:16: error: member reference base type 'StackNode<T> *' is not a structure or union
        T tempaT = top.getValue();
                   ~~~^~~~~~~~~
Stack.cpp:16:16: error: member reference base type 'StackNode<T> *' is not a structure or union
        top = tempNode.getPrev();
              ~~~~~~~~^~~~~~~~
Stack.cpp:22:12: error: use of undeclared identifier 'nullptr'
        if(top == nullptr) {
                  ^
Stack.cpp:23:10: error: use of undeclared identifier 'nullptr'
                return nullptr;
                       ^
Stack.cpp:25:13: error: member reference base type 'StackNode<T> *const' is not a structure or union
                return top.getValue();
                       ~~~^~~~~~~~~
Stack.cpp:29:12: error: use of undeclared identifier 'T'
void Stack<T>::clear() {
           ^
Stack.cpp:29:16: warning: extra qualification on member 'clear' [-Wextra-qualification]
void Stack<T>::clear() {
               ^
Stack.cpp:29:16: error: out-of-line definition of 'clear' does not match any declaration in the global namespace
void Stack<T>::clear() {
               ^~~~~
Stack.cpp:30:5: error: use of undeclared identifier 'top'
        if(top == nullptr) {return;}
           ^
Stack.cpp:30:12: error: use of undeclared identifier 'nullptr'
        if(top == nullptr) {return;}
                  ^
Stack.cpp:31:14: error: use of undeclared identifier 'top'
        clearHelper(top);
                    ^
Stack.cpp:32:2: error: use of undeclared identifier 'top'
        top = nullptr;
        ^
Stack.cpp:32:8: error: use of undeclared identifier 'nullptr'
        top = nullptr;
              ^
Stack.cpp:35:12: error: use of undeclared identifier 'T'
void Stack<T>::clearHelper(StackNode<T>* start) {
           ^
Stack.cpp:35:38: error: use of undeclared identifier 'T'
void Stack<T>::clearHelper(StackNode<T>* start) {
                                     ^
Stack.cpp:35:38: error: use of undeclared identifier 'T'
Stack.cpp:35:16: warning: extra qualification on member 'clearHelper' [-Wextra-qualification]
void Stack<T>::clearHelper(StackNode<T>* start) {
               ^
Stack.cpp:36:14: error: use of undeclared identifier 'nullptr'
        if(start == nullptr) {
                    ^
Stack.cpp:44:12: error: use of undeclared identifier 'T'
bool Stack<T>::isEmpty() const {
           ^
Stack.cpp:44:16: warning: extra qualification on member 'isEmpty' [-Wextra-qualification]
bool Stack<T>::isEmpty() const {
               ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
3 warnings and 20 errors generated.
🌐
UnKnoWnCheaTs
unknowncheats.me › forum › programming-for-beginners › 313361-identifier-nullptr-undefined.html
[Help] identifier "nullptr" is undefined
December 25, 2018 - When i build my cheat, I get identifier "nullptr" is undefined I'm building a KMDF driver I have Latest WDK version installed and WDF extens
🌐
Reddit
reddit.com › r/learnprogramming › [c++] program not compiling due to "nullptr not being declared in this scope".
r/learnprogramming on Reddit: [C++] Program not compiling due to "nullptr not being declared in this scope".
October 4, 2016 -

Hello,

I'm writing a program in which it generates a random number between 0-100 and the user has to guess. The program will respond with whether or not their guess is correct, too high, or too low, etc. However, in putty my source code is not compiling with the error lab2.cpp:13: error: 'nullptr' was not declared in this scope lab2.cpp:35: error: expected ';' before '}' token

My code is as follows:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int n = 0;
bool do_more = true;

srand(time(nullptr));
int target = rand() % 100 + 1;
cout << "Welcome to the number guessing game! " << endl;
cout << "Please enter your guess, between 0 and 100. " <<     endl;

do {
	cout << "Enter your guess: ";
	cin >> n;
	if (n == 0) {
		cout << "Bye! ";
		do_more = false;
	}
	else if (n > target) {
		cout << "Guess is too high! Try again! " << endl;
	}
	else if (n < target) {
		cout << "Guess is too low. Try again! " << endl;
	}
	else {
		cout << "You win! ";
		cout << "Answer is " << n << endl;
		do_more = false;
		}
} while (do_more);
return 0;
}

Thanks in advance for any help with this confusing error.

🌐
Omi AI
omi.me › blogs › firmware-guides › how-to-fix-error-nullptr-was-not-declared-in-this-scope
How to Fix Error: 'nullptr' was not declared in this scope – Omi AI
October 30, 2024 - In Visual Studio, update the project properties to use a C++ standard that supports `nullptr`. Navigate to Project Settings > C/C++ > Language and set the C++ Language Standard to a version supporting `nullptr`.
🌐
Reddit
reddit.com › r/cpp_questions › is p > nullptr or similar undefined behavior in c++?
r/cpp_questions on Reddit: Is p > nullptr or similar undefined behavior in C++?
February 3, 2024 -

Sorry if this question is really simple, but I've been trying to find out if (x > x), (x < x), (x >= x) and (x <= x) where one x is nullptr and the other x is a valid pointer is undefined behavior. I've been trying to test it with "-fsanitize=undefined" and it hasn't complained so far. So I just wanted some confirmation from people who know way more than me.

So for example, does this test code invoke undefined behavior?

#include <iostream>

struct CheckStatus
{
    bool first = false;
    bool second = false;
    bool third = false;
    bool fourth = false;
};

[[nodiscard]] CheckStatus Check()
{
    CheckStatus cs;
    char c1('a');
    char* cp1(&c1);
    char* cp2(nullptr);
    
    if(cp1 < cp2) cs.first = true;
    if(cp1 > cp2) cs.second = true;
    if(cp1 <= cp2) cs.third = true;
    if(cp1 >= cp2) cs.fourth = true;
    
    return cs;
}

int main()
{
    CheckStatus cs(Check());
    
    if(cs.first) std::cout << "1st: true" << std::endl;
    if(cs.second) std::cout << "2nd: true" << std::endl;
    if(cs.third) std::cout << "3rd: true" << std::endl;
    if(cs.fourth) std::cout << "4th: true" << std::endl;
    
    return 0;
}

(In case for some reason the code isn't formatted correctly for you, please see it here: "https://pastebin.com/nNetU7pu")

Top answer
1 of 6
9
The answer is… it depends. The rules for pointer relational comparisons are: If converted lhs and rhs are both pointers, pointer conversions, function pointer conversions(since C++17) and qualification conversions are performed on both converted operands to bring them to their composite pointer type. The two pointers of the composite pointer type are compared as follows: If the pointers compare equal or the equality comparison result is unspecified, the relational comparison result falls into the same category. Otherwise (the pointers compare unequal), if any of the pointers is not a pointer to object, the result is unspecified. Otherwise (both pointers point to objects), the result is defined in terms of a partial order consistent with the following rules: Given two different elements high and low of an array such than high has higher subscript than low, if one pointer points to high (or a subobject of high) and the other pointer points to low (or a subobject of low), the former compares greater than the latter. If one pointer points to an element elem (or to a subobject of elem) of an array, and the other pointer is past the end of the same array, the past-the-end pointer compares greater than the other pointer. If one pointer points to a complete object, a base class subobject or a member subobject obj (or to a subobject of obj), and the other pointer is past the end of obj, the past-the-end pointer compares greater than the other pointer. If the pointers point to different non-zero-sized(since C++20) non-static data members with the same member access(until C++23) of the same object of a non-union class type, or to subobjects of such members, recursively, the pointer to the later declared member compares greater than the other pointer. Otherwise, the result is unspecified. So, it’s not that it’s undefined behavior, it’s just unspecified behavior. Comparing unrelated pointers relationally cannot be relied on to produce any kind of meaningful result. But they are ultimately convertible to integers, so they are inherently always relationally compatible. nullptr is a special type since C++11 (before that it was the C-ish 0 value, but the problem with that is 0 is a valid address on some platforms). The value it represents when converted to a pointer type is unspecified. So no, you cannot rely on nullptr to relationally compare greater or less than any other pointer. Only not equal to all pointers not nullptr. In practice, it will compare consistently to other pointers, but it being greater or less cannot be relied on.
2 of 6
6
It is unspecified in the case of using <, and implementation defined in the case of using std::less : https://en.cppreference.com/w/cpp/language/operator_comparison#Pointer_total_order ;
🌐
Cplusplus
cplusplus.com › forum › beginner › 124613
syntax error - C++ Forum
February 25, 2014 - "nullptr" only works with C++ 11, and when I compiled this with C++ 11, it worked, but did not with C++ 98. By default compilers use C++ 98, which calls for 0, rather than nullptr.