31

Why is the condition in this code true?

int main ( )
{

   if ("")
      cout << "hello"; // executes!

   return 0;
}
chris
  • 60,560
  • 13
  • 143
  • 205
D Mehta
  • 433
  • 4
  • 11
  • 16
    Because `""` is a non-`NULL` pointer. – Jesper Jul 02 '14 at 12:17
  • 6
    `""` as an expression evaluates to a non-null address, non-null in boolean eval-speak means not-false. – WhozCraig Jul 02 '14 at 12:18
  • 6
    Actually I don't see a reason this should be so much downvoted. The question is good, it's to the point, the code is shown the author only needs an answer he's not aware of. Why so much hate? – lukas.pukenis Jul 02 '14 at 12:20
  • 2
    More robust ways to check `string`s would be `.empty()` or `.length()`. – Cory Kramer Jul 02 '14 at 12:20
  • @lukas.pukenis So if I post the exact same question with `if("false")`, `if("1")` and a thousand other similar questions, that would also be fine? – Pierre Arlaud Jul 02 '14 at 14:09
  • @lukas.pukenis In general, downvoting doesn't mean hate. It just means that a question is not good. But here you are right, it is a trivial question, but not necessarily a bad one. – glglgl Jul 02 '14 at 14:17
  • This question is very similar to: http://stackoverflow.com/questions/7155563/why-does-an-empty-string-have-an-address – Kris Jul 18 '14 at 11:44
  • Agree ... this is not a bad question, especially for folks who are used to languages like Perl and Python, where empty string evaluates to false. Even for an experienced C++ developer, if they have significant experience in those other languages it's easy to forget in the moment sometimes. – MarkR Oct 06 '21 at 20:31

3 Answers3

36

A condition is considered "true" if it evaluates to anything other than 0*. "" is a const char array containing a single \0 character. To evaluate this as a condition, the compiler "decays" the array to const char*. Since the const char[1] is not located at address 0, the pointer is nonzero and the condition is satisfied.


* More precisely, if it evaluates to true after being implicitly converted to bool. For simple types this amounts to the same thing as nonzero, but for class types you have to consider whether operator bool() is defined and what it does.

§ 4.12 from the C++ 11 draft spec:

4.12 Boolean conversions [conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

Community
  • 1
  • 1
dlf
  • 9,045
  • 4
  • 32
  • 58
  • 12
    It's a `const char[1]`. – chris Jul 02 '14 at 12:18
  • 2
    @chris It is, but to be evaluated as a condition it must be converted to `const char*`, correct? – dlf Jul 02 '14 at 12:22
  • 4
    Yes, it decays, but it is not a pointer to begin with. As the whole string literals being pointers thing is such a common misconception, I find it important to clarify. – chris Jul 02 '14 at 12:22
  • 1
    @chris closely related to my utter hatred for the word "decays", a *verb* that implies functional activity where in-fact none exists. +1 on the type-clarification btw. – WhozCraig Jul 02 '14 at 12:31
  • 1
    According to the C++ standard. Null pointers don't necessarily have to point to address 0. It's just an common implementation on many platforms. – TNA Jul 02 '14 at 12:32
  • 1
    @TNA: If I recall, the null pointer is the only one that evaluates to false in a boolean conversion context though. – Mooing Duck Jul 02 '14 at 18:50
  • @TNA The C++11 spec (§ 4.10.1) states "A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t." – dlf Jul 02 '14 at 19:18
  • @TNA *C*, however, did not have this restriction, and apparently there really were/are C compilers that use nonzero null pointers. See [this question and answer](http://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0), for example. – dlf Jul 02 '14 at 19:27
  • @dlf: Read that whole section, not just the first sentence. It goes on to say that "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 object pointer or function pointer type." That does not imply that the "null pointer value" of any type must be equivalent to a pointer to the address 0. – rici Jul 02 '14 at 23:28
4

Because "" decays to a char const* and all non-null pointers evaluate to true if or when converted to a boolean.

Niall
  • 30,036
  • 10
  • 99
  • 142
TNA
  • 2,595
  • 1
  • 14
  • 19
1

You are probably coming from a languange like PHP, where the check is processed different:

 php -r 'echo "X";if ("") echo "Y";'

THis will print the X, but not the Y because the empty string has no value.

As others have pointed out, in C++ it's a non-null-pointer, so evaluated as true.