char a[2] defines a char array. a is a pointer to memory at the beginning of the array, and using == will not actually compare the contents of a with 'ab' , because they are actually not the same types, 'ab' is an integer type. Also, 'ab' should be "ab" , otherwise you will also have problems. To compare char arrays, you want to use strcmp.
Something that might be illustrative is to look at typeid 'ab' :
#include <iostream> #include <typeinfo> using namespace std; int main(){ int some_int =5; std::cout << typeid('ab').name() << std::endl; std::cout << typeid(some_int).name() << std::endl; return 0; }
on my system, this returns:
i i
indicating that 'ab' actually evaluated as int.
If you were to do the same with std :: string, then you would come across a class and std :: string with operator == overloaded and do a match check when calling this method.
If you want to compare the input with the string "ab" in C ++ idiomatic mode, I suggest you do it like this:
#include <iostream>
shuttle87
source share