Last Minute C Programming Logical Operators Tutorial

c-logical-operators-tutorial

C programming language logical operators are often used along with conditional operators to combine two are more conditions. Use of conditional operators reduces the number of IF and ELSE statements and improves performance of C program. Also, the size of C program is considerably reduced.

Note: There are no keywords like AND, OR, NOT, TRUE and FALSE in c language.

NOT (!) operator works on single operand or expression where as AND (&&) and OR (||) operators require two operands.

C Programming Logical Operators

There are three logical operators in C language.

1. AND - && - Ampersand Ampersand

2. OR - || - Pipe Pipe

3 NOT - ! - Exclamation

Example 1: Without logical operators

int main()
{
  int b=10;
  if(b <= 10)
  {
    if(b >= 5)
    {
      printf("YES");
    }
  }
  
  return 9;
}
//output=YES

Here we used two IF statements with two conditions to reach YES output.

Example 2: With logical operators

int main()
{
  int b=10;
  if((b <= 10) && (b >=5))
  {
    printf("YES");
  }
  
  return 9;
}
//output=YES

Here two conditions are combined using && operator. Now the code looks simple and compiler takes less time to compile and the performance is improved.

Output Chart or Truth Table of C Logical Operators

Logical AND operation requires both operands to be true to give output as TRUE. Logical OR operation requires just once true operand to give output as TRUE. Logical NOT (!) simply makes TRUE to false and FALSE to true. Logical operators && and || are also called Short Circuit operators because they skip evaluating second operand if the outcome is already decided with first operand as TRUE.

Operand 1 OP1 Operand 2 OP2 OP1 && OP2 OP1 || OP2
false false false false
false true false true
true false false true
true true true true

Observation: Advantage of logical AND operator is that if left side operand (Operand 1) is false, it will not look the second operand and directly gives false output. Similarly, only if left operand is TRUE, right side operand is checked for true or false. Logical OR operator also skips the second operand if the first operand is TRUE. This greatly reduces time to solve second operand or second expression in many scenarios.

Hierarchy or Priority or Precedence of Logical Operators vs Conditional Operators vs Arithmetic Operators

NOT (!) operator has got the highest priority. Logical Operators have less priority than Arithmetic Operators. Look at the chart below. Conditional operators have more priority than logical operators.

Priority Operator Symbol Operator Name
1 ! Logical NOT
2 *, /, % Multiplication, Division, Modulo
3 +, - Addition, Subtraction
4 <, <=, >, >= Less than, Less than or equal to, Greater than, Greater than or equal to
5 ==, != Equal to, Not equal to
6 && Logical AND
7 || Logical OR
8 = Assignment

Example 3:

int main()
{
  int year = 2019;
  if((year%4 == 0) && (year%100!=0))
    printf("LEAP YEAR");
  else
    printf("NO");
  if(year%400 == 0)
    printf("LEA YEAR);
  
  return 9;
}
//output=NO

So, proper use of Arithmetic, Conditional and Logical operators ease programming and maintain large code bases.

C programming online tests on Conditional, Logical Operators or Statements

1  Conditional Statements Operators - Online Test 1
2  Conditional Statements Operators - Online Test 2