Mr.Adnan's blog

By Mr.Adnan, history, 5 days ago, In English,

I don't know if it's an error from C++ or not.

Try this two codes: I tried to compare two string "a" and "adnan", in two codes, it giving me two different outputs, where the second one is correct.

#include<bits/stdc++.h>
using namespace std;

int main()
{
  //  cout<<("a">"adnan")<<endl;
   cout<<("a"> "adnan" ? "a": "adnan")<<endl;

}
/* The output is: 
a

which is incorrect.
*/

and

#include<bits/stdc++.h>
using namespace std;

int main()
{
   cout<<("a">"adnan")<<endl;
   cout<<("a"> "adnan" ? "a": "adnan")<<endl;


}
/* the output is:
0
adnan
*/

which is correct.

I tried from different compilers I've tested it from codechef's compiler also: c++ 14, from ideone: C++, C++ 14

And found the same error. let me know reason please.

 
 
 
 
  • Vote: I like it  
  • +6
  • Vote: I do not like it  

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Mr.Adnan (previous revision, new revision, compare).

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Mr.Adnan (previous revision, new revision, compare).

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Mr.Adnan (previous revision, new revision, compare).

»
5 days ago, # |
  Vote: I like it +4 Vote: I do not like it

You should typecast first.

Something like this,

cout << (((string)"a"> (string)"adnan") ? "a": "adnan") << endl;

  • »
    »
    5 days ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Why typecast is needed? isn't it string by default? can you provide me any links to know more?

    • »
      »
      »
      5 days ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      String comparison works in stl only. How you be sure that compiler detect your input as stl string?

»
5 days ago, # |
  Vote: I like it +1 Vote: I do not like it

You have to convert it into a string type before comparing with one another.

string a = "a"; string adnan = "adnan"; cout << (a > adnan ? a : adnan) << endl;

It should provide your desired output.

»
5 days ago, # |
  Vote: I like it -21 Vote: I do not like it

"a" and "adnan" are not variables , so by comparing them there may occur some undefined behaviour ..

  • »
    »
    5 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Got it! Thanks!

  • »
    »
    5 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    But the second one is working, and it is the same as previous, right?

    • »
      »
      »
      4 days ago, # ^ |
        Vote: I like it -10 Vote: I do not like it

      The problem name describes it all "Undefined behaviour".

    • »
      »
      »
      4 days ago, # ^ |
        Vote: I like it +21 Vote: I do not like it

      "a" and "adnan" are the constants of type const char[N], that means they are arrays of chars. When you try to compare arrays, they are treated as pointers to the first element of the array (aka const char*), that means that they are compared by value of address, where array is stored. But result of such comparison is unspecified by standard. Read this answer if you want to know more about that https://stackoverflow.com/a/9086675 .

      • »
        »
        »
        »
        4 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        wow! thank you so much! Understood it. thanks again.