Search code examples
c++nulluser-defined-types

Define null as a lower case version of NULL? c++


Quick question here, I am not a huge fan of typing NULL always in upper case and I wanted to know if I could define a lower case version.

When I try to define it in the header file as:

#define null NULL;

it allows this line to be compiled but if I try and call null anywhere in my code I get a syntax error.

For example, if I write:

if(root == null){ /* Code here */}

Eclipse tells me that this is a syntax error and if I try and build my project I get this error:

null error

Is NULL a special case in which I would not be allowed to do this or am I doing something incorrect?


Solution

  • If you are using a C++11 compiler, use nullptr and don't bother with the #define.

    If you must use the #define, remove the ; from the line.

    #define null NULL