C Programming

Logical Operators in C

In C language Operator Groups are present. There are seven types of operators present. They are:

  1. Unary 
  2. Arithmetic
  3. Bitwise
  4. Relational
  5. Logical
  6. Conditional
  7. Assignment

In C there is a preceding rule that exists in case of operator Groups. If in a problem there are multiple operators present, then this type of problem is solved according to this order of operator groups.

Logical operator is the member of this operator groups. There are three types of logical operators present in C language.

  1. NOT ! ( 1st Priority )
  2. AND && ( 2nd  Priority )
  3. OR || ( 3rd Priority )

NOT operator

It is also unary operator. Priority level is same as of unary operators. It inverts the truth value of statement.

1
2
3
! T = F

! F = T

Programming Example 1

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main ()

{

    int  y,  x = 5 ;
    y =  !x> 4 ;


printf ( " Result is: %d ", y ) ;

}

Output

Here, three operators are used. As NOT operator has highest priority level, not operator executes first.  So, !x means not of non-zero value is 0. Now is 0 > 4? No. So the result is false ( zero ). It can be done with the help of greater than ( > ) operator. So, 0 is assigned in y with the help of assignment operator ( = ).

So, the result = 0.

Programming Example 2

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

int main ()

{

    int  a, b ;
printf (" Enter any number : " ) ;
scanf ( "%d ", &a ) ;

    b = !a ;
printf ( " The result is : %d ", b ) ;

    return 0 ;


}

Output

Explanation

The above programming example is of logical operator. Here, the given expression of logical operator is:

1
b = !a ;

Here two operators are used. One is assignment operator ( = ), another is logical not operator ( ! ). As the logical not operator is of higher priority than assignment operator, logical not operator executes first.

Here, we enter a number by the user. The number is 56. It is a non – zero value. Logical not operator turns into a zero. This zero is assigned to the x variable with the help of assignment operator.  So, the result will be zero.

Programming Example 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()

{

    int x =10 ;
    int y ;
    y = !( x !=10 ) ;


printf ( " Result is : %d ", y ) ;

}

Output

Explanation

The above programming example is of logical operator. Here, the given expression of logical operator is:

1
y = !( x !=10 ) ;

Here four operators are used.  Two logical not operator ( ! ) and rest of it is assignment operator ( = ). As NOT operator has highest priority level, not operator executes first. Not operator within the first parenthesis executes first. So, “x!” means not of non-zero value is 0. Now, this zero is assigned to x with the help of assignment operator. Now “!0” means a nonzero value, which is a true value. This true value again assigned to y variable with the help of assignment operator.

So, output of the result = 1.

AND operator

Statement 1 && Statement 2 Result
False X False
True False False
True True True

Programming Example 4

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main ()

{

    int  y,  x = 5 ;

    y = x>4  &&  x<10 ;

printf ( " Result is: %d ", y ) ;

}

Output

Here && operator combined these two conditions. If 1st statement is checked, if 1st statement = false, so the overall result = 0. Only if both two statements are true, then over all result is true, otherwise false.

In this program, both the statements are “True”. So, output of result = 1.

Programming Example 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main()

{

    // let us declare a variable a
    int a = 17 ;


printf ( " Result is: %d\n", (a>=20  && a<=50)) ;

    return 0 ;

}

Output

Explanation

The above programming example is another example of logical operator. Here, the given expression of logical operator is:

1
a>=20  && a<=50 ;

Here && operator combined these two conditions. If 1st statement is checked, if 1st statement = false, so overall result = 0. Only if both two statements are true, then overall result is true, otherwise false.

In this program, 1st statement is “True”. The second one is false. So, overall output of result = 0 as one statement is false.

Programming Example 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()

{

    // let us declare a variable a
    int a= 10;

    int b= a!=10 && a>=5;

printf("Result is: %d\n",(b));

    return 0;

}

Output

Explanation

The above programming example is another example of logical operator. Here the given expression of logical operator is:

1
b= a!=10 && a>=5;

Here, && operator combined these two conditions. If 1st statement is checked, if 1st statement = false, so overall result = 0. Only if both two statements are true, then overall result is true, otherwise false.

In this program, 1st statement is false. The second one is no need to check. So, overall output of result = 0 as one statement is false.

OR operator

Statement 1  &&  Statement 2 Result
False False False
False True True
True X True

Programming Example 7

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()

{

    int y, x =5;

    y= x < 4 || x < 10;

printf ("Result is: %d",y);

}

Output

Explanation

The above programming example is another example of logical operator. Here the given expression of logical operator is:

1
y= x < 4 || x < 10 ;

Here || operator combined these two conditions. In OR operator statement, if both statements are False, then only the overall result = False. If any statement is “True”, then the overall result is “True”.

In this program, 1st statement is “True” but 2nd statement is False. So, the overall result is true. So, the output of the result is 1.

Programming Example 8

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()

{

    int y , x = 10 ;

    y =  x!=10 || x >= 5 ;

printf ( "Result is: %d ",y ) ;

}

Output

Explanation

The above programming example is another example of logical and operator. Here, the given expression of logical operator is:

1
y =  x!=10 || x >= 5 ;

Here, || operator combined these two conditions. In OR operator statement, if both statements are False, then only the overall result = False. If any statement is true, then the overall result is true.

In this program, 1st statement is False, but 2nd statement is true. So, the overall result is true. So, the result is 1.

Conclusion

From the above discussion about the concepts of logical operators, we have come to the conclusion that logical operators give the programmer a variation of arising different types of condition that are applied in the program. Logical operators make this condition very easy to understand. So, it increases the readability of the program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com