3

I'm less than a year into C++ development (focused on other languages prior to this) and I'm looking at a guy's code who's been doing this for two decades. I've never seen this syntax before and hopefully someone can be of some help.

bool b; // There exists a Boolean variable.
int i;  // There exists an integer variable.

sscanf(value, "%d", &i); // The int is assigned from a scan.
b = (i != 0); // I have never seen this syntax before.

I get that the boolean is being assigned from the int that was just scanned, but I don't get the (* != 0) aspects of what's going on. Could someone explain why this person who knows the language much better than I is doing syntax like this?

Brad B.
  • 316
  • 2
  • 13
  • What is difficult about this? If `i != 0` then `b` is `true`, and if `i == 0` the `b` is `false`. The right-hand-side evaluates to a temporary `bool` that gets assigned to `b`. – Cory Kramer Mar 26 '15 at 17:45
  • It's not clear what you're asking about. You've never seen `=`? `()`? `!=`? `;`? – Lightness Races in Orbit Mar 26 '15 at 17:46
  • 1
    The comments on those first two lines are terrible :( – Lightness Races in Orbit Mar 26 '15 at 17:48
  • There's no need to be rude, Lightness Races in Orbit. I specifically was verbose in my commenting because people come here to learn how to program and I wanted to leave nothing to question if someone who has never programmed at all looked at this question. – Brad B. Mar 26 '15 at 18:15
  • @BradB: I'm not being "rude". I'm pointing out a fact. You will need to learn to accept constructive criticism! Being verbose and repeating yourself are not mutually exclusive and those comments _are_ terrible because they literally just repeat the code before them. It would be best not to teach/encourage others to write comments like that, if you think people come here to learn how to program. On the other hand, people _shouldn't_ come here to learn how to program: it is a Q&A, not a tutorial or learning book! HTH. – Lightness Races in Orbit Mar 26 '15 at 19:18

6 Answers6

2

Have a read here: http://en.cppreference.com/w/cpp/language/operator_comparison

The result of operator != is a bool. So the person is saying "compare the value in i with 0". If 'i' is not equal to 0, then the '!=' returns true.

So in effect the value in b is "true if 'i' is anything but zero"

EDIT: In response to the OP's comment on this, yes you could have a similar situation if you used any other operator which returns bool. Of course when used with an int type, the != means negative numbers evaluate to true. If > 0 were used then both 0 and negative numbers would evaluate to false.

qeadz
  • 1,476
  • 1
  • 9
  • 17
  • I've never seen a "!=" operated used this way before. Would it be the same situation if the person who wrote this code wrote (i > 0) instead of (i != 0)? If you add to your answer that operators may be used anywhere like this I'll select your answer as the correct one. – Brad B. Mar 26 '15 at 17:50
  • 1
    @BradB. For unsigned types is `(i > 0)` <=> `(i != 0)` but for signed it is not. (For signed it would be `(i < 0 || i > 0)` ) – Otomo Mar 26 '15 at 17:57
  • Follow up question. Would this work as well "b = (bool)i;"? I've done that all the time for going from doubles to ints e.g. "i = (int)d;". – Brad B. Mar 26 '15 at 18:03
  • I was trying to find a good article to link on this but a cursory google search didn't show up anything to the point. Yes you can do that but in general the boolean type should be considered very different to a type which stores a numerical value. So the operator expression makes it apparent how this conversion is to take place, and easy to change (if at some point you wanted i >= 0 instead of i != 0 or other for that matter). However every company I've ever worked for has had very few programmers who fuss about how the expressions are written, just so long as they function correctly. – qeadz Mar 26 '15 at 18:18
2

The expression (i != 0) evaluates to a boolean value, true if the expression is true (i.e. if i is non-zero) and false otherwise.

This value is then assigned to b.

You'd get the same result from b = i;, if you prefer brevity to explicitness, due to the standard boolean conversion from numeric types which gives false for zero and true for non-zero.

Or b = (i != 0) ? true : false; if you like extraneous verbosity.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

(i != 0) is an expression that evaluates to true or false. Hence, b gets the value of true/false depending on the value of i.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

This is fairly fundamental syntax. The != operator performs a "not equal to" comparison.

You may be being confused by the shorthand of initialising a bool directly from the result of a comparison operator, but the syntax itself is not esoteric.

The program is essentially equivalent to:

bool b;
int i;

sscanf(value, "%d", &i);

if (i != 0)
   b = true;
else
   b = false;

The key is that i != 0 is itself an expression that evaluates to true or false, not some magic that may only be used in an if statement.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Basically, if the condition (i not_equal_to 0 ) is satisfied, b gets the value "true". Else b gets the value "false".

Salim Mahboubi
  • 561
  • 7
  • 25
0

Here, "i != 0" is a boolean expression that will be true if "i" is non-zero and false if it is zero.

All that is happening here is the result of that expression is being assigned to a variable.

You could also do things like...

boolean canDrinkAlcohol = (person.age() >= 18 && person.country.equals("UK") || person.age() >= 21 && person.county.equals("US"));

...

if(canDrinkAlcohol) {
   ...
}

or something

BretC
  • 4,141
  • 13
  • 22