Menu
Coddy logo textTech

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:

OperatorMeaningExample
==Equal1 == 2 returns 0 (false)
!=Not Equal1 != 2 returns 1 (true)
>Greater Than1 > 2 returns 0 (false)
<Less Than1 < 2 returns 1 (true)
>=Greater or Equal1 >= 2 returns 0 (false)
<=Less or Equal1 <= 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 icon

Challenge

Easy

Write 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):

OperatorMeaningExample
==Equal1 == 2 returns 0 (false)
!=Not Equal1 != 2 returns 1 (true)
>Greater Than1 > 2 returns 0 (false)
<Less Than1 < 2 returns 1 (true)
>=Greater or Equal1 >= 2 returns 0 (false)
<=Less or Equal1 <= 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals