10

What happens if I compare two characters in this way:

if ('a' == 'b')
    doSomething();

I'm really curious to know what the language (and the compiler) does when it finds a comparison like this. And, of course, if it is a correct way to do something, or if I have to use something like strcmp().

EDIT Wait wait.
Since someone haven't understood what I really mean, I decided to explain in another way.

char x, y;
cout << "Put a character: ";
cin >> x;
cout << "Put another character: ";
cin >> y;

if (x == y)
    doSomething();

Of course, in the if brackets you can replace == with any other comparison operator.
What really I want to know is: how the character are considered in C/C++? When the compiler compares two characters, how does it know that 'a' is different than 'b'? It refers to the ASCII table?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Overflowh
  • 1,103
  • 6
  • 18
  • 40
  • 9
    A optimizing compiler will realize that this could never be true and eliminate the entire statement. – Marc B Nov 30 '11 at 18:09
  • 2
    @MarcB I suspect this may be an abstracted, simplified example using "magic" characters instead of variables. – Andrew Marshall Nov 30 '11 at 18:10
  • 1
    I don't think you're actually asking about the specific comparison of 'a' to 'b', since that would obviously always return False, but instead asking about comparing single characters. If so, please edit your question to make that clear. – Lee-Man Nov 30 '11 at 18:12
  • 1
    @MarcB I suspect the question is asking about the abstract case, and your comment addresses his specific example. – Lee-Man Nov 30 '11 at 18:13
  • 4
    Perhaps the OP is a recovering Java programmer and was expecting something like `(new Char('a')).equals(new Char('b'))` to be the correct approach (or perhaps something more sophisticated involving a wrapper factory). – Kerrek SB Nov 30 '11 at 18:18
  • `'a' == 'b'` is a great way of saying *false* in a language without `false`. – fredoverflow Nov 30 '11 at 18:52

9 Answers9

20

you can absolutely securely compare fundamental types by comparison operator ==

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
triclosan
  • 5,578
  • 6
  • 26
  • 50
  • 14
    Additionally, it should be pointed out that 'a' and 'b' aren't strings, they are characters (primitives). If it were "a" == "b" it would be a different story. – Eric Andres Nov 30 '11 at 18:10
  • 1
    You're almost right about fundamental types, except of floats/doubles. I think you understand, that `if ( doubleValue == 3.14 )` isn't correct in many situations – borisbn Feb 29 '12 at 09:09
  • @borisbn, you are not exactly right. `floats`/`doubles` are equal if theirs values are binary equal as well. In your example problem may occurs due to round-off errors not by `==` fault. Please note that even in this case operator `==` makes its job as it should. – triclosan Apr 24 '13 at 11:06
  • @triclosan computers **always** act exactly as you write, but sometimes `what did you write` != `what did you want to say to do` )) – borisbn Apr 24 '13 at 14:55
13

In C and C++, single character constants (and char variables) are integer values (in the mathematical sense, not in the sense of int values). The compiler compares them as integers when you use ==. You can also use the other integer comparison operators (<, <=, etc.) You can also add and subtract them. (For instance, a common idiom to change a digit character into its numerical value is c - '0'.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
5

For single chars, this form is correct. If both operands are known at compile time as in your example, then the condition can (and almost certainly will) be evaluated at compile time and not result in any code.

Note that a char ('a') is different from a single-character string ("a"). For the latter, comparison has a different meaning: it would compare the pointers rather than the characters.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
5

Your processor would subtract both operands and if it's zero, zero condition bit is set, your values were the same.

For example: on arm machines you have the nzcv (negative, zero, carry, overflow) bits which tell you what happened.

Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
user1073834
  • 103
  • 4
  • 1
    This probably isn't relevant. It's possible to implement a machine that does not have these properties, so it's inaccurate to say that these are established facts unless the questioner were to explicitly state the platforms (s)he is working on. (Which in the case of x86 and ARM, you'd be right, but I could easily devise an instruction set for which this does not hold.) – greyfade Nov 30 '11 at 19:14
  • 3
    mostly you have a status register and a mathematical operation. in my example its the subtraction and as far as i know most processors use this practice which could translate this hi level code to his own asm instructions. all i want to say is, that in case of a compare you have 2 registers with a value in it and you compare it by a mathematical operation. the question was "how does it know that 'a' is different than 'b'?" sorry if i understood something else – user1073834 Nov 30 '11 at 21:07
  • The question was how does C++ handle `'a' == 'b'`, not how does the machine handle it. In that regard, the machine's behavior is not relevant. – greyfade Nov 30 '11 at 23:19
1

Nothing will happen as a doesn't equal b.

If you question is just about is that the correct way, then the answer is yes.

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
1

First 'a' and 'b' are not strings, they are characters. The nuance is important because of its implications.

You can compare characters to characters just fine the same way you can compare integers to integers and floats with floats. It's usually not done because the outcome will always be the same. i.e. 'a' == 'b' will always be false.

If you're comparing strings, however, you'll have to use something like strcmp().

1

Compiler simply inserts an instruction for comparing two bytes for equality - a very efficient operation. Of course in your case 'a'=='b' is equivalent to a constant false.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The compiler will compare the numeric ASCII codes. So, 'a' is never equal to 'b'. But, 'a' < 'b' evaluates to true, since 'a' appears before 'b' in the ASCII table.

Gowtham
  • 1,465
  • 1
  • 15
  • 26
0

Of course, you want to use variables like

char myChr = 'a' ;
if( myChr == 'b' ) puts( "It's b" ) ;

Now you can start to think about "Yoda conditions", where you would do

if( 'b' == myChr )  puts( "It's a b" ) ;

so that in case you accidently typed one equals sign in the 2nd example:

if( 'b' = myChr ) puts( "It's a b" ) ;

that would raise a compiler error

Community
  • 1
  • 1
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • This just makes the code uglier, and any decent compiler will give you a warning for `if (x=5)`. – interjay Dec 01 '11 at 09:56
  • Hmm. I suppose it IS a bit hypocritical since I don't use Yoda conditions myself. Still, some people like them. – bobobobo Dec 01 '11 at 17:49