2

For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g.

int foo () {
    return (bar > 5);
}

The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do:

if (foo() == TRUE) { /* do stuff */ }

I know that the best option would be to simply do

if (foo())

but you never know.

Is there a defined standard value for boolean expressions or is it up to the compiler? If there is, is the standard value something included in C99? what about C89?

JACH
  • 996
  • 11
  • 20
  • 1
    there is a standard type `bool` with constants `true` and `false` defined in the standard header `stdbool.h` since C99. – Eugene Sh. Sep 27 '16 at 17:10
  • 2
    To complete what Eugene said, you might be interested by this link : http://en.cppreference.com/w/c/types/boolean – Thomas W. Sep 27 '16 at 17:11
  • Thanks. I'm actually looking for argumentative facts to 1)change that definition of TRUE, and 2) use a C99 compliant compiler (right now, we're using a C89 compliant one) – JACH Sep 27 '16 at 17:32

2 Answers2

6

An operator such as ==, !=, &&, and || that results in a boolean value will evaluate to 1 of the expression is true and 0 if the expression is false. The type of this expressing is int.

So if the TRUE macro is not defined as 1, a comparison such as the above will fail.

When an expression is evaluated in a boolean context, 0 evaluates to false and non-zero evaluates to true. So to be safe, TRUE should be defined as:

#define TRUE (!0)

As was mentioned in the comments, if your compiler is C99 compliant, you can #include <stdbool.h> and use true and false.

According to C99:

6.5.3.3 (Unary arithmetic operators)

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

6.5.8 (Relational operators)

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

6.5.9 (Equality operators)

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int.

6.5.13 (Logical AND operator)

The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

6.5.14 (Logical OR operator)

The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks. I expected that much. But I'm not sure if it evaluates to 1 by standard, or if that's something compiler-specific – JACH Sep 27 '16 at 17:32
  • @JACH Added addtional notes from C99 regarding boolean expressions. – dbush Sep 27 '16 at 17:38
  • Detail: the _type_ of the result is `int` with `==, !=, &&, ||, !, >, >=, <, <=` – chux - Reinstate Monica Sep 27 '16 at 17:41
  • Thanks for the information. So it is defined in C99. It seems that C89 information is hard to come by and or does not exist. I guess another reason to switch to C99. – JACH Sep 27 '16 at 18:21
  • I think you could change the wording of *"So to be safe, TRUE should be defined as: `#define TRUE (!0)`"* to something akin to *"If you cannot remember what the value of true is in C (it is 1), you can use `#define TRUE (!0)`"* because I've seen high-rep people suggesting that `!0` is not always 1. – Antti Haapala -- Слава Україні May 22 '18 at 16:31
0

The C programming language does not define Boolean value. Traditionally, the C programming language uses integer types to represent boolean data types.

Boolean values in C:

0 = false` 

Any other value = true`

Usually people will use 1 for true.

C99 introduced the_Bool data type that is not available in other C derivates.See wikipedia link here. Additionally, a new header stdbool.h has been added for compatibility reasons. This header allows programmers to use boolean types in the same way, as in C++ language.

To use bool in C, we can use enum as below.

enum bool {
    false, true
};

bool value;
value = bool(0); // False
value = bool(1); // True

if(value == false)
    printf("Value is false");
else
    printf("Value is true");

Also, related Stack overflow question Is bool a native C type?

Community
  • 1
  • 1
msc
  • 33,420
  • 29
  • 119
  • 214