Logical Operators in C programming
In C programming, logical operators are used to perform logical operations on expressions, typically involving Boolean values (true or false). They are often used in decision-making constructs like if, while, and for loops.
Note:
Any non-zero value is consider as true in c programming.
Precedence and Associativity of Logical Operators:
Operator |
Precedence Level |
Associativity |
! |
Highest |
Right-to-left |
&& |
Medium |
Left-to-right |
|| |
Medium |
Left-to-right |
Explanation of Associativity:
- Right-to-left (for
!
):
If multiple "!"
operators are used, evaluation starts from the rightmost operator.
example:
int a = 5;
printf("%d", !!!a); // Evaluates as !( !( !a ) )
- Left-to-right (for
&&
and||
):
When multiple "&&
" or"||
" operators are used in a single expression, they are evaluated from left to right.
Example:
int a = 10, b = 5, c = 20;
if (a > b && b < c && c > 15) {
printf("True");
}
a > b
(true), then b < c
(true), and finally c > 15
(true).
Basic questions:
Practice Questions:
- evaluate the expression
- evaluate the expression and explain in detail
-
evaluate the expression
What's Your Reaction?