Блог пользователя Mr.Adnan

Автор Mr.Adnan, история, 5 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

You should typecast first.

Something like this,

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

»
5 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится -21 Проголосовать: не нравится

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

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Got it! Thanks!

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится -10 Проголосовать: не нравится

      The problem name describes it all "Undefined behaviour".

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится +21 Проголосовать: не нравится

      "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 .