Logical Operators in C programming language return true(non-zero number) or false(0) value. Logical AND(&&) and logical OR(||) works on 2 operands. But logical NOT(!) works on single operand.
Related Read:
Relational Operators In C
Page Contents
Logical Operators
&& – Logical AND Operator.
|| – Logical OR Operator.
! – Logical NOT Operator.
&& – Logical AND Operator.
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( 1 && 1 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 1
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( 0 && 1 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 0
For logical AND(&&) both operands or expressions must yield to true. If any one condition is false(0), then it’ll return false(0).
|| – Logical OR Operator.
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( 1 || 1 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 1
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( 1 || 0 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 1
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( 0 || 0 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 0
Logical OR(||) returns true(any non-zero number) if either one condition/operand is true. It returns false(0) only when both the conditions / operands are false(0).
! – Logical NOT Operator.
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( !1 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 0
- #include < stdio.h >
- int main()
- {
- int a;
- a = ( !0 );
- printf("value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 1
Logical NOT(!) returns true if the condition is false. It returns false if the condition is true. It just negates the Boolean value given to it.
Logical Operators In C
Logical Operators combined with Relational Operators
&& – Logical AND Operator.
- #include < stdio.h >
- int main()
- {
- int a, b = 100;
- a = ( (b == 0) && (b > 50) );
- printf("Value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 1
a is true because both b is equal to 100 is true and b is greater than 50 is true.
- #include < stdio.h >
- int main()
- {
- int a, b = 100;
- a = ( (b == 0) && (b > 150) );
- printf("Value of a is %d\n", a);
- return 0;
- }
Output:
value of a is 0
a is false(0) because b is equal to 100 is true but b is greater than 150 is false.
|| – Logical OR Operator.
- #include < stdio.h >
- int main()
- {
- int a, b = 100;
- a = ( (b == 0) || (b > 50) );
- printf("%d\n", a);
- return 0;
- }
Output:
value of a is 1
Value of a is true, because b is equal to true. In logical OR(||) if one condition is true, then it returns true. It returns false(0) only when both the conditions / operands are false(0).
! – Logical NOT Operator.
- #include < stdio.h >
- int main()
- {
- int a, b = 100;
- a = ( !(b == 0) );
- printf("%d\n", a);
- return 0;
- }
Output:
value of a is 0
Value of a is false(0). Because b is equal to 100 is true. When true value is given to NOT(!) it’ll return false(0). When false value is supplied to NOT it’ll return true.
Note: = is assignment operator. == is equality operator.
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert