Reputation: 31
I want to compare two char types in C++. I tried cout<<"x"=="x";
to see the result and it wont work(which I believe is normal), so I tried converting it by trying int letter = "x"
to try to compare it by it ASCII number. This gets me the error;
error:invalid conversion from 'const char*' to 'char'
Shouldn't this work? If not, what should I be doing?
Upvotes: 0
Views: 199
Reputation: 21
"x"
is an array of chars - string - (2 bytes - one for char x
, one for char \0
nul)'x'
is char - variable represented by 1 byte in memory.Assigning "x"
to an int
variable is an obvious mistake.
Try 'x'
instead of "x"
: int letter = 'x';
should work fine.
Upvotes: 2
Reputation: 493
You are using double quotes around your characters.
Double quotes are of type const char*, not type char
Try
int letter = 'x';
Upvotes: 1
Reputation: 42165
"x"
gives you a nul-terminated array of characters {'x','\0'}
.
Use 'x'
if you want a single char
Upvotes: 5