'\0' evaluates false, "\0" evaluates true '\0' evaluates false, "\0" evaluates true c c

'\0' evaluates false, "\0" evaluates true


Recall how string literals work in C - "\0" is a character array containing two zero bytes (the one you asked for, and the implicit one at the end). When evaluated for the if test, it decays into a pointer to its first character. This pointer is not NULL, so it's considered true when used as a condition.

'\0' is the number zero, equivalent to just 0. It's an integer which is zero, so it's considered false when used as a condition.


First of all, you need to keep in mind that in C,

  • Zero is false and non-zero is true.
  • For pointer types, NULL is false and non-NULL is true.

'\0', as others have said, is the same as the integer literal 0 and hence is false (See first bullet point above to know why).

"\0" is a string literal that contains two \0 characters (One which you have explicitly added and the other, which is implicit and will be added by the compiler). The string literal will be stored somewhere in read-only memory. When you use "\0", it gets converted to a pointer to its first element. This is commonly referred to as "array decay". (This is the reason why stuff like char* str = "string"; works).

So, you are effectively checking the address of the first character of the string literal. Since the address of the string literal will always be non-NULL, the if will always be true (See second bullet point above to know why).


: This "decay" of arrays does not always happen. See Exception to array not decaying into a pointer?


'\0' is a number: 0, so it is evaluated as false (0 = false, !0 = true).

But "\0" is a pointer to a read-only section where the actual string is stored, the pointer is not NULL ergo it's true.