38

I think this is a simple question, but I'm struggling with the following. In my example I have the following statement (language is C):

int foobar

if (foobar)
{
// do something.
}

Now, if I am correct about this, this statement is true when foobar is not zero. So it should be much the same as if (foobar!=0).

But what happens if foobar becomes a negative number?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Bart Teunissen
  • 1,420
  • 5
  • 20
  • 43
  • 3
    Possible duplicate of [Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?](https://stackoverflow.com/questions/4276207/is-c-c-bool-type-always-guaranteed-to-be-0-or-1-when-typecasted-to-int) – phuclv Aug 29 '18 at 09:32
  • 1
    [What is the boolean value of integers other than 0 or 1?](https://stackoverflow.com/q/27024044/995714), [Is it ok to assume 0 is false and 1 is true?](https://stackoverflow.com/q/33756721/995714) – phuclv Aug 29 '18 at 09:33
  • @phuclv How can an older post be a duplicate of a newer? – SHR Aug 29 '18 at 13:25
  • @SHR [*The general rule is to keep the question with the best collection of answers, and close the other one as a duplicate*](https://meta.stackexchange.com/q/10841/230282). Posted time isn't considered here. But the other question was postedi n 2010, how can it be newer than this? – phuclv Aug 29 '18 at 14:27
  • I don't understand why this question was not closed at the time. OP already understood that the behaviour depends on whether `foobar` is equal to zero or not. So the only remaining question is "are negative numbers equal to zero?" which... is a math question and not a programming question, to the extent that it makes any sense at all (if you can't answer that, then how can you have heard of negative numbers in the first place?). – Karl Knechtel Nov 29 '20 at 20:22

3 Answers3

66

negative or positive. Anything that's not a 0 is a true value in if

Also, Consider a negative number: -1

-1 in C internally is represented as: 0xFFFFFFFF, in which case, it would be a positive number if I cast it to unsigned integer.

But after the advent of C99 standard compilers, I suggest you use <stdbool.h> instead. Makes the guessing work a lot less:

Read here about stdbool.h

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 4
    Representation of negative numbers is implementation-defined. Also it does not matter to this question, only the *value* matters, regardless of how it is represented. The `int` is not converted to `unsigned int` in this context. – M.M May 16 '17 at 01:45
  • @M.M technically everything's "Implementation Defined" - unless you plan to implement it for a particular machine. What if I implement a machine that cannot represent Zero? That's why the `` :) – Aniket Inge Sep 09 '18 at 01:08
  • You clearly don't know what "implementation-defined" means. If there is a machine that can't represent 0, then it cannot have a conforming C compiler . – M.M Sep 09 '18 at 03:13
  • @M.M could you point me in the ISO document where it says "If there is a machine that can't represent 0, then it cannot have a conforming C compiler" – Aniket Inge Sep 10 '18 at 01:53
  • See C11 5.2.4.2.1/1 . For example, `int` is required to be able to store all values in the range [-32767, +32767] , which includes 0 – M.M Sep 10 '18 at 06:02
  • @M.M so that means, for a conforming C compiler, the size of `int` has to be minimum of 2 byte length? – Aniket Inge Sep 10 '18 at 06:18
  • 1
    It has to be minimum of 16-bit length (the size of 1 byte may be more than 8 bit) – M.M Sep 10 '18 at 06:20
6

same, the

if (foobar) 

means foobar not zero so whether it is positive or negative doesn't matter, it is still not zero

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

Your statement should return true if foobar is a negative number (it's still different than zero) but you should avoid that sort of test as it's not the best practice to test variables that can have different 'true' values in that way.

Lupuss
  • 649
  • 2
  • 11
  • 21
  • that is just what i'm trying to avoid, that it gets to a unknow state. I saw this line of code in someone elses program and then it hit me. why use that statement instead of if(foobar!=0); now i know. Thanks all for anwsering my question! – Bart Teunissen Feb 01 '13 at 13:04