NULL
and '\0'
are guaranteed to evaluate to 0, so (with appropriate casts) they can be considered identical in value; notice however that they represent two very different things: NULL
is a null (always invalid) pointer, while '\0'
is the string terminator. EOF
instead is a negative integer constant that indicates the end of a stream; often it's -1, but the standard doesn't say anything about its actual value.
C & C++ differ in the type of NULL
and '\0'
:
- in C++
'\0'
is a char
, while in C it's an int
; this because in C all character literals are considered int
s.
in C++ NULL
is "just" an integral 0, while in C it can be defined as a 0 casted to void *
; this cannot be done in C++ (and it's explicitly forbidden in a note) because, being C++ more strict in pointer conversions, a void *
is not implicitly convertible to any other pointer type, so, if NULL
was a void *
, it would be necessary to cast it to the target pointer type on assignment:
int * ptr = (void *) 0; /* valid C, invalid C++ */
Relevant standard quotations:
C++98/03
NULL
NULL
is an integer type guaranteed to evaluate to 0:
4.10 Pointer conversions
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer
to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).
18.1 Types
[...] The macro NULL is an implementation-defined C++ null pointer constant in this international Standard (4.10). (Possible definitions include 0
and 0L
, but not (void*)0
).
'\0'
A 0-value char must exist:
2.2 Character sets
The basic execution character set and the basic execution wide-character set shall each contain [...] a null character (respectively, null wide character), whose representation has all zero bits.
'\0'
is a char
literal:
2.13.2 Character literals
A character literal is one or more characters enclosed in single quotes, as in 'x'
, optionally preceded by the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char
, with value equal to the numerical value of the encoding of the c-char in the execution character set.
and it's value is 0, since that escape sequence specifies its value:
The escape \ooo
consists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character.
'\0'
is used to terminate strings literals:
2.13.4 String literals
After any necessary concatenation, in translation phase 7 (2.1), '\0'
is appended to every string literal so that programs that scan a string can find its end.
EOF
The definition of EOF
is delegated to the C89 standard (as stated in §27.8.2 "C Library files"), where it is defined as an implementation specific negative integer.
C99
NULL
A null pointer is a 0 integer, optionally casted to void *
; NULL
is a null pointer.
6.3.2.3 Pointers
[...] An integer constant expression with the value 0, or such an expression cast to type
void *
, is called a null pointer constant. (The macro NULL
is defined in <stddef.h>
(and other headers) as a null pointer constant; see 7.17.) 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.
7.17 Common definitions <stddef.h>
[...] The macros are
NULL
which expands to an implementation-defined null pointer constant; [...]
'\0'
'\0'
is an integer with value 0, and is used to terminate strings:
5.2.1 Character sets
[...] 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.
6.4.4.4 Character constants
An integer character constant is a sequence of one or more multibyte characters enclosed
in single-quotes, as in 'x'
. [...]
The octal digits that follow the backslash in an octal escape sequence are taken to be part of the construction of a single character for an integer character constant or of a single wide character for a wide character constant. The numerical value of the octal integer so formed specifies the value of the desired character or wide character. [...]
An integer character constant has type int
.
EOF
EOF
is an implementation-defined negative integer
7.19 Input/output <stdio.h>
7.19.1 Introduction
EOF
which expands to an integer constant expression, with type int
and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a
stream