C – Logical Operators

      Prev                                                                                              Next

Logical operators in C:

    • These operators are used to perform logical operations on the given expressions.
    • There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
S.no
Operators
Name
Example
Description
1
&&
logical AND
(x>5)&&(y<5)
It returns true when both conditions are true
2
||
logical OR
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
3
!
logical NOT
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false

 Example program for 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 truen");
     }
     if (o>p || p!=20) 
     {
          printf("|| Operator : Only one condition is truen");
     }
     if (!(m>n && m !=0)) 
     {   
         printf("! Operator  : Both conditions are truen");  
     }
     else
     {   
         printf("! Operator  : Both conditions are true. " 
                "But, status is inverted as   falsen");  
     }
}

Output:

&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
    • In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.
    • && operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true. Else, it becomes false.
    • || Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.
    • ! Operator  – It is used to reverses the state of the operand.
    • If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator.
    • So, “! (m>n and m! =0)” returns false (0).

……

 Continue on types of C operators:

    • Click on each operators name below to display example programs.
S.no
Types of Operators
             Description              
1
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
2
Assignment_operators These are used to assign the values for the variables in C programs.
3
Relational operators These operators are used to compare the value of two variables.
4
Logical operators These operators are used to perform logical operations on the given two variables.
5
Bit wise operators These operators are used to perform bit operations on given two variables.
6
Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false.
7
Increment / decrement operators These operators are used to either increase or decrease the value of the variable by one.
8
Special operators &, *, sizeof( ) and ternary operators

      Prev                                                                                              Next

.