Boolean operators in C

    The logical operators are also called Boolean operators. The name Boolean comes from George Boole, who first described the branch of mathematics that works with truth values, instead of numbers.

Introduction to boolean operators

Advertise on this site. I promise you will like the rates :)

C has three logical operators:

  • && - logical AND, also called "conjunction"
  • || - logical OR, also called "disjunction"
  • ! - logical NOT

They all work with truth values and return as result truth value.

Note:If you are coming from another language like Java or C#:
Do not confuse the boolean operators && and || with the operators & and |. In C, the & and | operators are only bitwise.

Recall from the data types lesson, that C recognizes as true any value that is not 0 and the 0 is false.
Also, if you are writing C99 compatible code, you can and should use the <stdbool.h> and the macros that it defines: true and false. For more information see the data types lesson.

&& = boolean AND = Conjunction

The boolean operator && returns true only when all its operands are true. let's visualize this:

A B A && B
false false false
false true false
true false false
true true true

which is the same as

A B A && B
0 0 0
0 1 0
1 0 0
1 1 1

Let's create an example that asks the user for his/her age and gender and determines if that is a woman under 30.
In this example we use boolean values returned by the comparison operators "<" and "==".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>

int main(void)
{
    int age;
    char gender;
	
    printf("This program demonstrates how we can use logical AND to combine two conditions in a single statement.\n");
	
    printf("Enter your age:");
    scanf("%d", &age);
	
    printf("Enter your gender, m/f: ");
    scanf(" %c[^\n]", &gender);

    if(age < 30 && gender == 'f')
        printf("Yes, this is a female under 30.\n");
    else
        printf("No, this is not a female under 30.\n");
		
    return 0;
}

|| = boolean OR = Disjunction

The logical operator || returns true, if at least one of the operands is true. If they are all false, it returns false. Here's its truth table:

A B A || B
false false false
false true true
true false true
true true true

which is equivallent to

A B A || B
0 0 0
0 1 1
1 0 1
1 1 1

Let's make another short example: Input two numbers and check if at least one of them is not a zero:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>

int main(void)
{
    int num1;
    int num2;
	
    printf("This program returns true if the first OR the second number is not 0.\n");
	
    printf("Enter num1 :");
    scanf(" %d", &num1);
	
    printf("Enter num2 :");
    scanf(" %d", &num2);

    if(num1 || num2)
        printf("Yes, at least one of the nubmers is non-zero.\n");
    else
        printf("No, both nubmers are zeros.\n");
		
    return 0;
}

Here is how this code works for me:

Remember: in the C language 0 means false and anything else is true. That is why we don't need to compare the numbers to 0.

The code from the examples above is also available on Git Hub.

! = Logical NOT = Negation

The negation always returns the opposite of its operand. NOT true is false. NOT false is true. Simple logic ;)

Precedence

The boolean operators have different precedence. The operations are done from left to right and from the highest to the lowest precedence.
From highest to lowest they are:

  • Logical NOT
  • Logical AND
  • Logical OR

This means that:

    !true || true && !false

will be equal to true. The first operation to be evaluated is the the highest precedence - the first logical NOT. This will transform the above expression to:

    false || true && !false

Now the && will be evaluated, because it has a higher precedence, compared to the logical OR. However, the right operand of the logical AND contains logical NOT which has an even higher precedence, so first that will be calculated:

    false || true && true

Only now the logical AND can return its result:

    false || true

.. and finally the logical OR will return true.

To change the order of execution we use braces. Even if braces are not necessary to change the order of evaluation, they can be used to make it more obvious what is expected to happen. It is a good coding practice to place braces in complex expressions (more than 2 operators in one statement).
The following expression will first do the operations in the braces and only then it will do the rest:

    (true || false) && !false

Previous: switch

Next: Loops in C

   Search this site: