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?

3
  • 3
    What is your question?
    – chqrlie
    May 8, 2015 at 10:11
  • Please refer to this before answering. May 8, 2015 at 10:22
  • 2
    Why is it marked as duplicate? Perhaps the question was badly written but this is not a duplicate. The other thread doesn't have NUL in its title.
    – Bilow
    Mar 6, 2018 at 13:49

3 Answers 3

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'
2
  • 2
    If you do, it might look like this: #define NUL '\0'
    – chqrlie
    May 8, 2015 at 10:18
  • @chqrlie: Good point. Appropriate changes made
    – Levi
    May 8, 2015 at 10:20
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.

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')

2
  • In C++ a char will never be of the same size as an int because chars are always 1 byte, and ints are always at least 2 bytes.
    – Oli
    May 8, 2015 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 8, 2015 at 10:28