Logical operators in C

C programming logical operators; In this tutorial, i am going to show you logical operators in c programming and how to use logical operators in c.

There are three types of logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

Logical Operators in C

Logical operators are used to performing logical operations of given expressions (relational expressions) or variables in c programming language.

Types of Logical Operators in C

The following list of logical operators in c programming; is as follows:

OperatorMeaningExample
&&Logical AND. True only if all operands are trueIf c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
||Logical OR. True only if either one operand is trueIf c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
!Logical NOT. True only if the operand is 0If c = 5 then, expression !(c==5) equals to 0.

Example 1 – Logical Operators in C

#include<stdio.h>
int main()
{
    int a = 5, b = 10 ,ret;
    ret = ( (a <= b) || (a != b) ); 
    // 5 <= 10 ==> true. 5 != 10 ==> true. So, 1 || 1 will return 1.
    printf("Return value of above expression is %d\n",ret);
    ret = ( ( a < b) && (a == b ) ); 
    // 5 < 10 ==>true. 5 == 10 ==> false. So, 1 && 0 will return 0;
    printf("Return value of above expression is %d\n",ret);
    ret = ! ( ( a < b) && (a == b ) ); 
    /*we have used the same expression here.
     And its result was 0.
     So !0 will be 1.*/
    printf("Return value of above expression is %d\n",ret);
    return 0;
}

Output

Return value of above expression is 1
Return value of above expression is 0
Return value of above expression is 1

Example 2 – Logical Operators in C

#include <stdio.h>
 
int main()
{
   int m=40,n=20;
   int o=20,p=30;
   if (m>n && m !=0)
   {
      printf("&& Operator : Both conditions are true\n");
   }
   if (o>p || p!=20)
   {
      printf("|| Operator : Only one condition is true\n");
   }
   if (!(m>n && m !=0))
   {
      printf("! Operator : Both conditions are true\n");
   }
   else
   {
      printf("! Operator : Both conditions are true. " \
      "But, status is inverted as false\n");
   }
}

Output

&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false

Example 3 – Logical Operators in C

// Working of logical operators
#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;
    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);
    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);
    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);
    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);
    result = !(a != b);
    printf("!(a != b) is %d \n", result);
    result = !(a == b);
    printf("!(a == b) is %d \n", result);
    return 0;
}

Output

(a == b) && (c > b) is 1 
(a == b) && (c < b) is 0 
(a == b) || (c < b) is 1 
(a != b) || (c < b) is 0 
!(a != b) is 1 
!(a == b) is 0 

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *