CSC 161 Grinnell College Fall, 2011
Imperative Problem Solving and Data Structures

Reading: Boolean Values and Expressions

Boolean Type

If you prefer to work with true and false rather than 1 and 0, consider using the stdbool.h header. It defines three macros for true, false and the bool type. You can view the header with the following command:

more /usr/lib/gcc/i486-linux-gnu/4.4.5/include/stdbool.h

* Note that the bool type is only defined in the C99 standard. K&R uses the C89 standard which has no bool type. Make sure your compiler supports C99 if you use stdbool.h. For maximum compatibility, mimic the stdbool.h header with your own internal macros and integers for bool values.

With the stdbool.h header included, it is possible to use the bool type. Refferences to bool translate to _Bool after the preprocessor has its way with your code. _Bool is a primative type native to the C99 standard. It is used for storing boolean values/states. A bool variable can either be true or false.

Using Booleans

Comparison and Logical Operators

Conditional if statements operate on boolean values. An expression such as:

1 == 2;

returns true if the statement is correct, and false otherwise. The above expression evaluates to false. This is the standard behavior of all the comparison and logical operators in C: ==, <=, >=, <, >, !=, &&, and, ||.

It is easy to misuse the comparison operators. The expression:

-2 < -1 < 0

makes sense and would evaluate to true in mathematics. In C, this is not the case. Comparison operators associate left to right and evaluate to boolean values. The above expression in C actually evaluates to false. The value returned from the first comparison is 1 (true) because -2 is less than -1. Next, 1 is compared to 0. 1 is not less than 0 so the expression evaluates to false.

To do multiple tests at once, use logical AND (&&), and logical OR (||) operators. The previous example can be correctly expressed in C as:

-1 > -2 && -1 < 0


Conditional statements (ifs) are designed to work with boolean values. If an expression is true, the specified code will execute. In addition to all the logical and comparison operators in C, many functions also return boolean values to indicate different outcomes.

It is a good idea to avoid testing against boolean values in your conditionals if a value is already considered to be boolean. If a function or expression is supposed to return true on success and false on failure, do not further test that result against true or false. Consider the following:

if ( true )


if ( true == true )

Initially, it may seem the second case is more accurate than the first. But, add a third case:

if ( true == true == true )

The third case is no more descriptive or accurate than the second in the same way the second is no more accurate than the first. It is good practice to avoid this kind of redundancy in your testing.

Returning Boolean Values

Take advantage of the fact that comparison and logical operators in C evaluate to boolean values when returning boolean results from functions. Another common trap to avoid when working with boolean tests is the tendancy to write code that looks like this:

if ( x == y )
  return 1;
  return 0;

But that code is inefficient and word. Think about how operators which return boolean values (such as the == operator) work. If x and y have the contain values, then the result of the comparison x == y is true — which in C is represented by the value 1. If the comparison is false, the result is false, represented by the value 0. Thus, the return values are exactly what C computes with the statement x == , and the complex statement above can be simplified to

return ( x == y );