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.

  • 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? – Lundin Nov 15 '19 at 09: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 – Federico klez Culloca Nov 15 '19 at 09:34
  • Related: https://stackoverflow.com/questions/3577580/using-null-in-c – Marc.2377 Nov 15 '19 at 09: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. – Lundin Nov 15 '19 at 09:52

3 Answers3

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
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.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 1
    This is not *quite* complete. C++ *must* define NULL, but it is **not** allowed to define it as `(void*)0`. – Bathsheba Nov 15 '19 at 09:59
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.

Mike
  • 4,041
  • 6
  • 20
  • 37
varungupta
  • 155
  • 4
  • 10
  • Indeed , it is the ASCII associated to it. But '\0' is not the same as '0'. – varungupta Nov 15 '19 at 09:38
  • "use (x == NULL) if x is a pointer" is not the right suggestion for c++ anymore – 463035818_is_not_an_ai Nov 15 '19 at 09:39
  • 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. – Bathsheba Nov 15 '19 at 09:40
  • Null termination is often referred to as "nul" just to not mix it up with null pointers. – Lundin Nov 15 '19 at 09:46