Relational Operators
Part of the Fundamentals section of Coddy's C journey — lesson 20 of 63.
Comparison operators are used to compare two operands.
Sometimes we need to check whether an operand is bigger/smaller/... than another operand.
The following table shows possible operators for comparison:
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | 1 == 2 returns 0 (false) |
| != | Not Equal | 1 != 2 returns 1 (true) |
| > | Greater Than | 1 > 2 returns 0 (false) |
| < | Less Than | 1 < 2 returns 1 (true) |
| >= | Greater or Equal | 1 >= 2 returns 0 (false) |
| <= | Less or Equal | 1 <= 2 returns 1 (true) |
The comparison operator returns 1 if the comparison is true or 0 if it's false.
For example:
int var1 = 13;
int var2 = 12;
int var3 = var1 != var2;var3 will hold 1 because var1 and var2 are not equal
Challenge
EasyWrite a program that initializes 2 variables n1 and n2 with the values 8 and 9 (accordingly).
After that initialize another variable n3 that will hold whether n1 is greater than n2.
Print the values of all three variables using printf() in the following format:
n1 = [value of n1], n2 = [value of n2], n3 = [value of n3]Cheat sheet
Comparison operators are used to compare two operands and return 1 (true) or 0 (false):
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | 1 == 2 returns 0 (false) |
| != | Not Equal | 1 != 2 returns 1 (true) |
| > | Greater Than | 1 > 2 returns 0 (false) |
| < | Less Than | 1 < 2 returns 1 (true) |
| >= | Greater or Equal | 1 >= 2 returns 0 (false) |
| <= | Less or Equal | 1 <= 2 returns 1 (true) |
Example usage:
int var1 = 13;
int var2 = 12;
int var3 = var1 != var2; // var3 will hold 1 (true)Try it yourself
#include <stdio.h>
int main() {
// Type your code below
// Don't change the line below
printf("n1 = %d, n2 = %d, n3 = %d\n", n1, n2, n3);
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge