x

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 (!).
Operators
Example/Description
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (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:

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 operator name below for detailed description and example programs.


Types of Operators 
Description
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
Assignment_operators These are used to assign the values for the variables in C programs.
These operators are used to compare the value of two variables.
These operators are used to perform logical operations on the given two variables.
These operators are used to perform bit operations on given two variables.
Conditional operators return one value if condition is true and returns another value is condition is false.
These operators are used to either increase or decrease the value of the variable by one.
&, *, sizeof( ) and ternary operators.

Prev     Next



Like it? Please Spread the word!