You are here:

C++/Is the NULL a value in C/C++ ?



So can I just return zero instead of NULL for the same purpose?


Followup To
Question -

Is the NULL a value in C/C++ since the following return statment is not correct? I thought it was the same as the void.

#include <stdio.h>

int main()

void testMethod() {
   printf ("Hello World!\n");
   return NULL;

Answer -

Although strictly speaking we can use 0, as 0 (an integer value) is convertible to a null pointer value if assigned to a pointer type.

NULL is a macro in C defined in the header <stddef.h>, and C++ inherits it (and of course places it in the header <cstddef>, as per the usual mapping between C header names and C++ header names). Note that at one time C seems to have placed NULL in <stdio.h>

The definition of NULL for C and C++ differs; here is an example from the stddef.h header supplied with MSVC++ 8 (note: cstddef wraps and includes stddef.h in this implementation):

       /* Define NULL pointer value */
       #ifndef NULL
       #ifdef __cplusplus
       #define NULL    0
       #define NULL    ((void *)0)

As you can see in C++ NULL is just defined as 0, whereas in C it is defined as 0 cast to a void * pointer.  

Yes. Assuming that your purpose is to return a pointer and that pointer has a null pointer value.

In this case the result would be the same, an error. NULL and zero are not the same as no returned value at all, which is what is meant by a void return value as you specify in your shown code:

       void testMethod()

Note that this is not an object method. It is a stand alone function, so the function name testMethod might be misleading or confusing. Also, I should point out that C++ does not use the term method. It has member functions of one sort or another.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.