-1

Why C not bigger than b, when B bigger than a?

Code in Visual C++ 2017 STL:

void main ()
{       
    cout << ("A" > "A") << endl;
    cout << ("a" > "A")<< endl;
    cout << ("B" > "a")<< endl;
    cout << ("b" > "B")<< endl;
    cout << ("C" > "b")<< endl; 
    cout << ("c" > "C")<< endl;
    cout << ("AA" > "c")<< endl;
    cout << ("Aa" > "AA")<< endl;
    cout << ("aA" > "AA")<< endl;
    cout << ("aa" > "aA")<< endl;
    cout << ("BA" > "aa")<< endl;
    cout << ("Ba" > "BA")<< endl;
    cout << ("D" > "c")<< endl;
    string pause;
    cin >> pause;
}

OutPut: 0 1 1 1 0 1 1 1 1 1 1 1 1

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
justromagod
  • 933
  • 9
  • 20

3 Answers3

6

You have made an assumption that > and < perform lexicographic comparison on string literals.

Unfortunately, that assumption does not hold. You are just comparing pointers.

Use strcmp instead and, next time, read the documentation instead of making assumptions.

Also, main returns int, not void.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Relational operators for string [link](http://www.cplusplus.com/reference/string/string/operators/) http://www.cplusplus.com/reference/string/string/operators/ Performs the appropriate comparison operation between the string objects lhs and rhs. The functions use string::compare for the comparison. These operators are overloaded in header – justromagod Jul 12 '17 at 13:07
  • @justromagod: You are not using `std::string` anywhere in the code. – Lightness Races in Orbit Jul 12 '17 at 13:55
  • (well, except for the unrelated `pause` at the end... which, by the way, [you should not do](https://stackoverflow.com/a/36374595/560648)) – Lightness Races in Orbit Jul 12 '17 at 13:59
  • https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/. I found answer about issue, I belive it is leaky abstraction in C++ – justromagod Jul 25 '17 at 21:15
  • @justromagod I don't entirely disagree – Lightness Races in Orbit Aug 30 '18 at 15:41
3

You are comparing pointers.

"A" is a pointer to chararaters. (Actaully const char[2] as pointed out in comments) 'A' is a character.

Try this:

int main ()
{
    cout << ('A' > 'A') << endl;
    cout << ('a' > 'A')<< endl;
    cout << ('B' > 'a')<< endl;
    cout << ('b' > 'B')<< endl;
    cout << ('C' > 'b')<< endl; 
    cout << ('c' > 'C')<< endl;
}

You now need to think about what you want from a string (or char array).

Is "AA" less or greater than "c"? It comes first in a dictionary, but it's shorter.

If you want "dictionary" (lexographical) order you need to compare strings; strcmp or look at string comparision operators (if you decide to use std::string instead)

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

In the statements like these

cout << ("A" > "A") << endl;
cout << ("a" > "A")<< endl;
cout << ("B" > "a")<< endl;
//...

there are used string literals that have the static storage duration. In C++ string literals have types of constant character arrays. For example the string literal "A" has type const char[2] because in memory it is stored like { 'A', '\0' }.

Execute this statement

std::cout << sizeof( "A" ) << std::endl;

and you will see that the result will be 2.

From the C++ Standard (2.13.5 String literals)

8 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

In expressions like this "A" > "A" arrays are implicitly converted to pointers to their first characters. So in this expression there are compared two pointers.

From the C++ STandard (4.2 Array-to-pointer conversion)

1 An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The temporary materialization conversion (4.4) is applied. The result is a pointer to the first element of the array.

In general it is not necessary that string literals with equal contents will be stored as one character array. So in fact this expression

"A" == "A"

can yield either true or false depending on compiler options that is whether these two string literals stored as one array or as two distinct arrays by the compiler.

It seems your compiler stores equal string literals as one array. In this case this statement

cout << ("A" > "A") << endl;

outputs 0.

Also it seems that the compiler placed the array that stores the string literal "a" after the array that stores string literal "A". So the address of the first character of the string literal "a", that is of the character 'a', is higher than the address of the first character of the string literal "A". So this statement

cout << ("a" > "A")<< endl;

outputs 1.

Also the compiler placed the array that stores the string literal "B" after the array that stores the string literal "a". So again the address of the first character of the string literal "B" is higher than the address of the first character of the string literal "a" and in this statement

cout << ("B" > "a")<< endl;

there are compared these addresses. So the statement outputs 1.

And so on.

It seems what you need to do is to compare character literals instead of the string literals. For example

cout << ( 'A' > 'A') << endl;
cout << ('a' > 'A')<< endl;
cout << ('B' > 'a')<< endl;
//...
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335