C programming tutorial
Relational operators are used to compares two values.Output of a relational operator is true or false.For example (a>b). > one type of relational operator. Its output will be true(1) if a will be greater then b and its output will be false(0) if a would be less than b.
Following are the relational opertators in C.
| Operator | Meaning |
|---|---|
| > | Greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
| != | not equal to |
| == | equal to |
#include<stdio.h>
main()
{
int a=2,b=3;
printf("%d",(a>b)); // output will be zero since a is not greater than b
printf("%d",(a<b)); //output will be one since a is less than b
printf("%d",(a>=b)); //output will be zero since a is not greater or equal to b
printf("%d",(a<=b));//output will be one since a is less than b
printf("%d",(a!=b)); //output will be 1 since a is not equal to b
printf("%d",(a==b)); //ouput will be zero since a is not equal to b
}
Output
010110
What is the differnce between assignment operator(=) and equal to(==) operator ?
Often beginners get confused between assignment operator and equal to operator.There is big difference between them.
a=6;
Here assignment operator will assign 6 to variable a.
(a==6)
Here equal to operator will compare is a equal to 6 or not.
#include<stdio.h>
main()
{
int a=6;
printf("%d",a);
printf("%d",(a==6));
}
Output:
61
first printf will print the value of variable a i.e 6.Second printf will compare is 'a' equal to 6.Since a is equal to 6 output will be true(1).