Because C strings don't exist as such. They are char arrays ending in a \0
.
The equality operator ==
will test that the pointer to the first element of the array are the same. It won't compare lexicographically.
On the other hand "-hello" == "-hello"
may return non zero, but that doesn't mean that the ==
operator compares lexicographically. That's due to other facts.
If you want to compare lexicographically, you can always do this:
#define STR_EQ(s1,s2) \
strcmp(s1,s2) == 0
I see that you tagged as C++. So you could do this:
std::string arg1 ( argv[1] );
if (arg1 == "-hello"){
// yeahh!!!
}
else{
//awwwww
}