Relational Operators in C
Relational operators are used to compare values of two similar expressions. It returns Boolean vlaues either true or false depending on relation. If the expression is true, it returns 1 and if the expression is false, it returns 0.
Relational operators are binary operators because they require two operands to operate. It is used in decision making and loops. We take decisions or execute some statements based on evaluated Boolean result.
There are six relational operators in C, that compare two operands and return a boolean value either 0 (flse) or 1(true).
Operator | Description |
---|
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Equals to |
!= | Not equal to |
Let's have a look at the below example to understand each relational operators.
Suppose a, b and c are three integer variables with initial values.
int a=10, b=5, c=10;
Relational operations | Result | Description |
---|
a > b | 1 | 10 > 5 is true, therefore returns 1 |
a < b | 0 | 10 < 5 is false, therefore returns 0 |
a >= c | 1 | 10 ≥ 10 is true, therefore returns 1 |
a <= b | 0 | 10 ≤ 5 is false, therefore returns 0 |
a == c | 1 | 10 = 10 is true, therefore returns 1 |
a != c | 0 | 10 ≠ 10 is false, therefore returns 0 |
Remember: please don't be confused assignment =
operator with relational equals to ==
operator. Both are different and their working mechanism are different. Beginners always makes this common mistake of using =
in place of ==
. Hence use these with cautious.
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.