Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do negative numbers return false in C/C++?

Tags:

c++

c

boolean

When evaluating integers as booleans in C/C++, are negative numbers true or false? Are they always true/false regardless of compilers?

like image 995
Jason Maldonado Avatar asked Sep 17 '13 02:09

Jason Maldonado


People also ask

How does C handle negative numbers?

The C standard doesn't mandate any particular way of representing negative signed numbers. In most implementations that you are likely to encounter, negative signed integers are stored in what is called two's complement. The other major way of storing negative signed numbers is called one's complement.

Can you have negative integers in C?

C and C++ are unusual amongst languages nowadays in making a distinction between signed and unsigned integers. An int is signed by default, meaning it can represent both positive and negative values.

Can char store negative values in C?

The signed char type can store , negative , zero , and positive integer values . It has a minimum range between -127 and 127 , as defined by the C standard .

What integer is false in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.


2 Answers

All non-zero values will be converted to true, and zero values to false. With negative numbers being non-zero, they are converted to true.

Quoting from the C++11 standard (emphasis mine):

4.12 Boolean conversions [conv.bool]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.


Are they always true/false regardless of compilers?

You will only get the above guarantee when your compiler is standards-compliant, or at least, complies with this specific part of the standard. In practice, all compilers have this standard behavior, so there isn't much to worry about.

like image 123
Mark Garcia Avatar answered Sep 29 '22 16:09

Mark Garcia


You can test it yourself by compiling this:

#include <stdio.h>  int main(int argc, char** argv) {     if (-1) {         printf("-1 is true\n");     } else {         printf("-1 is false\n");     }     return 0; } 

Results:

$ gcc -Wall -pedantic test.c -o test-c
$ g++ -Wall -pedantic test.c -o test-cpp
$ ./test-c
-1 is true
$ ./test-cpp
-1 is true

Of course, to answer the second part of your question, "Are they always true/false regardless of compilers?", the only way to be completely sure is to look at the spec. In general though, compilers will warn you if you do something dangerous, and you can see from the output above, that even with "pedantic" warnings, gcc considers this code to be perfectly fine.

like image 29
Brendan Long Avatar answered Sep 29 '22 15:09

Brendan Long