• C Programming Video Tutorials

Logical Operators in C



Like the relational operators, the logical operators in C also evaluate to either true or false. The relational operators compare two numeric values. However, In C, the logical operators are typically used with Boolean operands. The logical AND operator, represented by && (double ampersand) symbol; and the logical OR operator, represented by || (double pipe) symbol, both are binary in nature (require two operands). The logical NOT operator (the ! symbol) is a unary operator. Since C treats 0 as false and nonzero as true, any operand to a logical operand is converted to a Boolean data.

Here is a table showing the logical operators in C −

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B)
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B)
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A)

The result of a logical operator follows the principle of Boolean algebra. The logical operators follow the following truth tables.

&& Operator

The && operator in C acts as a logical AND operator with the following truth table

a b a&&b
true true True
true false False
false true False
false false False

The above truth table shows that the result of && is true only if both the operands are true.

|| Operator

C uses || (double pipe symbol) for logical OR operator with the following truth table −

a b a||b
true true True
true false True
false true true
false false false

The above truth table shows that the result of || operator is true when either of the operands is true, and false if both operands are false.

! Operator

In C, the ! (exclamation mark) symbol is defined as a logical NOT operator with the following truth table −

A !a
True False
False True

The above truth table shows that the ! operator negates the value of Boolean operand, true becomes false, and false becomes true.

Unlike the other two logical operators && and ||, the logical NOT operator ! is a unary operator.

Example

The following example shows the usage of logical operators in C:

#include <stdio.h>

int main() {

   int a = 5;
   int b = 20;

   if ( a && b ) {
      printf("Line 1 - Condition is true\n" );
   }
	
   if ( a || b ) {
      printf("Line 2 - Condition is true\n" );
   }
   
   /* lets change the value of  a and b */
   a = 0;
   b = 10;
	
   if ( a && b ) {
      printf("Line 3 - Condition is true\n" );
   } else {
      printf("Line 3 - Condition is not true\n" );
   }
	
   if ( !(a && b) ) {
      printf("Line 4 - Condition is true\n" );
   }
   return 0;	
}

Output

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

In C, a char type is a subset of int type. Hence, logical operators can work with char type also.

Example

#include <stdio.h>

int main() {

   char a = 'a';
   char b = '\0'; // Null character

   if ( a && b ) {
      printf("Line 1 - Condition is true\n" );
   }

   if ( a || b ) {
      printf("Line 2 - Condition is true\n" );
   }
   return 0;
}

Output

Line 2 - Condition is true

The logical operators are generally employed to build a compound boolean expression. Along with the relational operators, the logical operators are also used in decision control and looping statements in C.

Example

The following example shows a compound Boolean expression in a C program −

#include <stdio.h>

int main() {

   int phy = 50;
   int maths = 60;

   if (phy<50 || maths<50)
   {
      printf("Result:Fail");
   }
   else
   {
      printf("Result:Pass");
   }
   return 0;
}

Output

Result:Pass

The similar logic can also be expressed with && operator as follows −

#include <stdio.h>

int main() {

   int phy = 50;
   int maths = 60;

   if (phy>=50 && maths>=50)
   {
      printf("Result:Pass");
   }
   else
   {
      printf("Result:Fail");
   }
   return 0;
}

Output

Result:Pass

Example

The following C code employs the NOT operator in a while loop −

#include <stdio.h>

int main() {

   int i=0;

   while (!(i>5))
   {
      printf("i = %d\n", i);
      i++;
   }
   return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5

In the above code, the while loop continues to iterate till the expression !(i>5) becomes false, which will be when the value of I becomes more than 5.

C has bitwise counterparts of the logical operators. The & (single ampersand) symbol is a bitwise AND operator. The | (single pipe) is a bitwise OR operator and ~ (tilde) symbol acts as a binary NOT or complement operator.

c_operators.htm
Advertisements