4

I've only ever seen "==" being used inside an if statement. So how does "==" work in this context?

a = 5;
b = (a == 18 % 13);
Kyle Morgan
  • 660
  • 1
  • 11
  • 21

4 Answers4

7

If b is a bool, you can assign the result of an expression to it. In this case, if the condition a == 18 % 13 holds, b will become true, otherwise false.

Basically,

a == 18 % 13 - would yield b = true or b = 1

and

a != 18 % 13 - would yield b = false or b = 0

depending on the type of b.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

This

a == 18 % 3

is equivalent to

a == (18%3)

since the modulus operator % has higher precedence than the equality operator ==.

This expression evaluates to true or false (actually, true in this case). So you are assigning the result of that to variable b. b itself could be a bool or anything that can be converted from bool.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • +1 for mentioning the precedence rules, which C++ has a boatload of. Bottom line: *Never* write code the way presented in the question. My rule: Everyone knows multiplication and division have precedence over addition and subtraction. You don't need parentheses for `a*b+3`, but spaces do help: `a*b + 3`. Parentheses are mandatory for anything else, and that includes `a==18%13`. – David Hammen Oct 17 '12 at 16:16
  • @DavidHammen I agree entirely, and I must admit that I had to look up the rules for this case to be sure of my answer. – juanchopanza Oct 17 '12 at 16:25
1

Ok, let's break it down..

The question is:

a = 5, b=(a==18%13); // What is b?

We'll start with the brackets. The %, called the modulus operator, gives you the remainder of dividing the two numbers. So 18/13 gives you 1 remainder 5. So:

18%13 = 5;

// so now we have
b=(a==5);

now the equivalency operator == can only return true or false, 1 or 0. It's the same as asking if the left operand is equivalent to the right operand. In this case:

 5 == 5; returns true or 1;

Therefore b = 1;

Patrick Borkowicz
  • 1,206
  • 10
  • 19
-1

C and C++ are not that high-level. They have no true boolean type (although in C++ and C99 there are typedefs for providing some small-width integer type to act as a boolean), so any nonzero integer, floating-point or pointer value is treated as boolean true and zero as false. As a consequence, logical expressions evaluate to either 1 (true) or 0 (false) and thus can be assigned to an integer.

  • I didn't downvote, but I suspect it's because there is actually a boolean type in both C and C++. In C its `_Bool` (although that is typedef'd to `bool` in one stdbool.h) and it's `bool` in C++. – Alex Oct 17 '12 at 18:24
  • @Alex "They have no true boolean type" <- _Bool & co. are not really booleans, because they're in fact disguised integers. –  Oct 17 '12 at 18:25
  • 2
    Trying to stuff an integer into a C `_Bool` or a C++ `bool` will result in it being truncated down to a 1 or 0. Both boolean types are guarenteed by the standard to always be 1 or 0. [See here](http://stackoverflow.com/questions/4276207/is-c-c-bool-type-always-guaranteed-to-be-0-or-1-when-typecasted-to-int) – Alex Oct 17 '12 at 18:30
  • This is wrong; C and C++ both have true boolean types – M.M Dec 10 '17 at 03:02