I\'m a c++ newbie (just oldschool c). My son asked for help with this and I\'m unable to explain it. If he had asked me \"how do I compare strings\" I would have told hi
In C++, the results are unspecified. I will be using N3337 for C++11.
First, we have to look at what the type of a string literal is.
§2.14.5
9Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type "array of nconst char", where n is the size of the string as defined below, and has static storage duration (3.7).
Arrays are colloquially said to decay to pointers.
§4.2
1An lvalue or rvalue of type "array ofN T" or "array of unknown bound ofT" can be converted to a prvalue of type "pointer toT". The result is a pointer to the first element of the array.
Since your string literals both contain one character, they're the same type (char[2], including the null character.)
Therefore the following paragraph applies:
§5.9
2[...]Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result defined as follows:
[...]
— If two pointers
pandqof the same type point to different objects that are not members of the same object or elements of the same array or to different functions, or if only one of them is null, the results ofp,p>q,p<=q, andp>=qare unspecified.
Unspecified means that the behavior depends on the implementation. We can see that GCC gives a warning about this:
warning: comparison with string literal results in unspecified behaviour [-Waddress]
std::cout << ("Z" < "A");
The behavior may change across compilers or compiler settings but in practice for what happens, see Wintermute's answer.