Weird Inequality

I saw some weird code lately, it looks something like this:

1
2
3
4
5
6
7
	bool mybool = 1;
	int myint1 = 2;
	int myint2 = 2;

	mybool = myint1 != myint2;

	cout << mybool << endl;


The output is 0, never seen this one before, how does it work exactly? My guess is it works like this:
mybool = !(myint1 != myint2) which is even weirder.
Last edited on
Let's put some parentheses in there to make it clearer what's happening:
mybool = (myint1 != myint2);
So it evaluates myint1 != myint2 first. Since both are equal, that means myint1 != myint2 is false.

So it assigns false to mybool.

Line 7 outputs mybool. False gets converted to 0 and that's what you see. I believe there's a way to make it output "true" or "false" but I've never used it.
Ah, That makes sense now, thanks.
Topic archived. No new replies allowed.