-1

I would like to understand the result of the below code in Microsoft visual studio C/C++ (2012 version). Indeed, when the solution is generated, the result is “1” (True). However, the word “a” is less than the word “z” in ASCII table. So, the result should be “0” (False). Even if I inverse the operation, mean ("z" > "a"). The result is “1”. I tried also this operation ("a" < "z") and ("z" < "a"), the result was “0” for the both Anyone can explain me what’s happening?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout <<  ("a" >  "z") << endl;
}
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Dohko
  • 1
  • 1
    It's a pointer compare between the addresses of the strings, not their contents. – Bo Persson Feb 11 '16 at 16:11
  • Look at this question in SO: http://stackoverflow.com/questions/7125697/c-comparing-pointers-with-chars. This talks about C, but your behavior in C++ is the same. – seva titov Feb 11 '16 at 16:12

1 Answers1

0

Your code is almost the same as if you had written:

const char s1[2] = {'a'};
const char s2[2] = {'z'};

int main()
{
    cout << (s1 < s2) << endl;
}

So you can see that your code is actually comparing the addresses of two character arrays.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131