2

Are both null and NULL constants the exact same thing in C/C++?

I've noticed as I write my code in Visual Studio that null and NULL don't get the same syntax highlighting, and the compile outcome is not the same as having null instead of NULL in some sections of the code.

4
  • 8
    There is no "C/C++", they are different languages and behave differently in this case. Please narrow down your question to one specific language. Also, what is null, Java? Commented Nov 15, 2019 at 9:33
  • 2
    "and the compile outcome is not the same as having null instead of NULL in some sections of the code" I think you answered your own question Commented Nov 15, 2019 at 9:34
  • Related: stackoverflow.com/questions/3577580/using-null-in-c Commented Nov 15, 2019 at 9:35
  • To make things perfectly clear, C has null pointers, null pointer constants, the NULL macro and null termination. All meaning different things. The integer constant 0 can be any of them. We may write it as 00 or \0 as well, which is octal notation. We always use octal notation when referring to null terminators, because we can - there exists no rationale why. You can also just as well use hex 0x0 or \x0, it means exactly the same thing as all of the above, but if you do, I will no longer have a clue what you are referring to. Commented Nov 15, 2019 at 9:52

3 Answers 3

15

They might be, they might not be.

Both C and C++ define NULL, but in slightly different ways.

null is not part of either standard; it is neither a keyword nor a reserved word, so you can use it as a variable name, class name &c..

The preferred way of denoting pointer null-ness in C++ is nullptr which has type especially designed for pointer nullness.

Sign up to request clarification or add additional context in comments.

9 Comments

You can find null under the sea
@SombreroChicken: You can also find it in my office on a Friday afternoon.
@SombreroChicken and seanulls at the shore
I've found a nice picture with a toilet paper roll, explaining what null vs empty can be ¯_(ツ)_/¯
@Bathsheba i.redd.it/3370lkxk56ny.jpg (you need the title of the reddit post for context , reddit.com/r/geek/comments/6128y3/…)
|
2

C standard says :

6.3.2.3/3 Pointers

An integer constant expression with the value 0, or such expression cast to type void *, is called a null pointer constant.

stddef.h then contains the NULL-define to that value.

C++ (another language, roughly related to C, certainly not in the way people usually think) used NULL in its very early versions (don't use it!). But in pre-C++11 releases, the constant 0 was defined as the way to represent pointers to nothing. Alas, this has some serious drawbacks and then C++11 defined the nullptr constant. Note that nullptr is a keyword.

C++ standard says:

2.14.17/1 Pointer literals

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.]

3.9.1/10 Fundamental types

A value of type std::nullptr_t is a null pointer constant. Such values participate in the pointer and the pointer to member conversions. sizeof(std::nullptr_t) shall be equal to sizeof(void*).

About NULL in C++ standard says:

18.2/3 Types

The macro NULL is an implementation-defined C++ null pointer constant.

1 Comment

This is not quite complete. C++ must define NULL, but it is not allowed to define it as (void*)0.
0

C language has a NULL but there is no null. NULL is used to indicate the macro defined in and when discussing pointers.

use null when discussing the null character. null character refers to '\0'.

With that semantics out of the way, if you are asking for the difference between (x == '\0') and (x == NULL) it would depend on what x is. Use (x == '\0') if x is a char or int and (x == NULL) if x is a pointer.
For everything else you'd have to consider whether the comparison is valid and how things will be converted or promoted.

4 Comments

Indeed , it is the ASCII associated to it. But '\0' is not the same as '0'.
"use (x == NULL) if x is a pointer" is not the right suggestion for c++ anymore
No that's not correct. In C, '\0' is exactly the same as 0 (both int types). In C++ the former is a char type, the latter an int type. Even more of a fun fact for the pub: 0 is an octal literal / constant.
Null termination is often referred to as "nul" just to not mix it up with null pointers.