3

NULL is a macro defined in <stddef.h> for the null pointer; it can be defined as ((void*)0). NULL is the name of the first character in the ASCII character set. What is the difference between them?

ameyCU
  • 16,489
  • 2
  • 26
  • 41

3 Answers3

8

NULL and NUL are of the same concept: They both represent the absence of a value. The only difference is - as you said - NULL is a macro in whereas NUL is the name given to the first ASCII character. The only scenario you are likely to come across a macro called NUL is something like this:

#define NUL '\0'
Levi
  • 1,921
  • 1
  • 14
  • 18
2

There is no such term as NUL in the C Standard. In the C Standard there are used the following terms

null character (\0)
NULL macro
null pointer
null pointer constant
null preprocessing directive
null statement
null wide character

For example

...A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

Or

3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Note that 0, NULL, '\0' and L'\0' are somewhat different:

sizeof(NULL) is the same as sizeof(void*), which is usually 8 on 64 bit Intel systems.

sizeof(0) is the same as sizeof(int), which is commonly still 4 on common 64 bit Intel systems.

sizeof('\0') is also the same as sizeof(int) in C, but is the same as sizeof(char) in C++ which has the value 1 by definition and is most likely different from sizeof(int).

sizeof(L'\0') is the same as sizeof(wchar_t), which is implementation defined.

Surprisingly, in C, you may have sizeof(L'\0') < sizeof('\0')

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • In C++ a `char` will never be of the same size as an `int` because `char`s are always 1 byte, and `int`s are always at least 2 bytes. – Oli May 08 '15 at 10:21
  • This makes sense as C++ must be able to distinguish between `char` and `int`, but even on systems where `sizeof(int) == 1`, `int` and `char` are distinct types in `C`, so why have this extra constraint? – chqrlie May 08 '15 at 10:28