- 2073 Shrawan · 4 marks
Relational and logical operators
Short answer
Relational operators (<, <=, >, >=, ==, !=) compare values and return 1 (true) or 0 (false). Logical operators && (AND) true if both true, || (OR) true if at least one true, ! (NOT) inverts. Example: (a<b)&&(b<c) returns 1 if a<b<c.
Explain relational and logical operators.
Relational operators
They compare two values and give 1 (true) or 0 (false).
| Operator | Meaning | Example (a=10, b=20) | Result |
|---|---|---|---|
< | less than | a < b | 1 |
<= | less than or equal | a <= b | 1 |
> | greater than | a > b | 0 |
>= | greater than or equal | a >= b | 0 |
== | equal to | a == b | 0 |
!= | not equal to | a != b | 1 |
Logical operators
They combine conditions and give 1 or 0.
| Operator | Meaning | Rule |
|---|---|---|
&& | AND | true only if both are true |
|| | OR | true if at least one is true |
! | NOT | reverses the truth value |
Example program:
#include <stdio.h>
int main(void)
{
int a = 10, b = 20, c = 30;
printf("a<b: %d, a==b: %d, a!=b: %d\n", a < b, a == b, a != b);
printf("(a<b)&&(b<c): %d\n", (a < b) && (b < c));
printf("(a>b)||(b<c): %d\n", (a > b) || (b < c));
printf("!(a<b): %d\n", !(a < b));
return 0;
}
Output:
a<b: 1, a==b: 0, a!=b: 1
(a<b)&&(b<c): 1
(a>b)||(b<c): 1
!(a<b): 0
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Relational and assignment operators · Computer Programming, chapter 3
- Relational operators comparing lengths · Object Oriented Programming, chapter 5
- Distance class relational operators · Object Oriented Programming, chapter 5
- Complex numbers relational operators · Object Oriented Programming, chapter 5