There are three logical operators in C - AND (&&) , OR (||), NOT (!). Logical operators in C language return 1 when an expression on which the operator is applied turns out to be true and 0 if it is false.
If you have any prior knowledge on Boolean Algebra, then it is easier to understand the working of these operators in C language.
Note: Zero(0) signifies false, and any value other than zero signifies true
Let us look at an example to illustrate this
Output:

Let us see what's happening with the last printf statement.
Expression: !(( a + b) - 15 ) && 1 )
- AND (A && B) - returns 1 when both A and B are true
- OR (A || B) - returns 1 when either A or B is true
- NOT !(A) - returns 1 when an expression is false, and 0 when the expression is true. It reverses/toggles the result.
If you have any prior knowledge on Boolean Algebra, then it is easier to understand the working of these operators in C language.
Note: Zero(0) signifies false, and any value other than zero signifies true
Let us look at an example to illustrate this
#include <stdio.h> int main() { int a = 5; int b = 10; printf (" A && B = %d\n", a&&b); printf (" A || B = %d\n", a||b); printf (" !A = %d\n", !a); printf (" A + B is 15 = %d\n", !((a+b) - 15) && 1); return 0; }
Output:

Let us see what's happening with the last printf statement.
Expression: !(( a + b) - 15 ) && 1 )
- a is 5 and b is 10, a + b is 15
- a + b - 15 results in 0
- !0 gives 1
- 1 && 1 gives 1
Discussion of Logical Operators ends here. You can try changing the values and experiment a little bit with logical operators. They are used in Decision making statements along with Relational operators for doing some operation when certain condition is met. You will learn about them in the coming chapters.
Sign up here with your email
ConversionConversion EmoticonEmoticon