Lesson 16 of 60 – Relational Operators in C
27%

Relational Operators in C

Relational operators are used to compare two values or expressions in C. They are commonly used in decision-making statements such as if, if-else, and loops.

Note: A relational expression evaluates to 1 when the comparison is true and 0 when the comparison is false.

1. What are Relational Operators?

Relational operators compare two operands and determine the relationship between them.

int a = 10;
int b = 20;

printf("%d", a < b);

Since 10 is less than 20, the expression produces 1.

2. Relational Operators in C

Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

3. Equal to Operator ==

The == operator checks whether two values are equal.

int a = 10;
int b = 10;

printf("%d", a == b);

Output:

1

The result is 1 because both values are equal.

4. Equal to with Different Values

int a = 10;
int b = 20;

printf("%d", a == b);

Output:

0

The result is 0 because 10 and 20 are not equal.

5. Not Equal to Operator !=

The != operator checks whether two values are different.

int a = 10;
int b = 20;

printf("%d", a != b);

Output:

1

The result is 1 because the values are different.

6. Not Equal with Same Values

int a = 10;
int b = 10;

printf("%d", a != b);

Output:

0

The result is 0 because the values are equal.

7. Greater Than Operator >

The > operator checks whether the left operand is greater than the right operand.

int a = 20;
int b = 10;

printf("%d", a > b);

Output:

1

8. Greater Than with False Condition

int a = 10;
int b = 20;

printf("%d", a > b);

Output:

0

The expression is false because 10 is not greater than 20.

9. Less Than Operator <

The < operator checks whether the left operand is less than the right operand.

int a = 10;
int b = 20;

printf("%d", a < b);

Output:

1

10. Less Than with False Condition

int a = 30;
int b = 20;

printf("%d", a < b);

Output:

0

The expression is false because 30 is not less than 20.

11. Greater Than or Equal to >=

The >= operator checks whether the left operand is greater than or equal to the right operand.

int marks = 60;

printf("%d", marks >= 40);

Output:

1

The condition is true because 60 is greater than 40.

12. Greater Than or Equal to with Equal Values

int marks = 40;

printf("%d", marks >= 40);

Output:

1

The condition is also true when both values are equal.

13. Less Than or Equal to <=

The <= operator checks whether the left operand is less than or equal to the right operand.

int age = 18;

printf("%d", age <= 18);

Output:

1

14. Less Than or Equal to with Smaller Value

int age = 15;

printf("%d", age <= 18);

Output:

1

The condition is true because 15 is less than 18.

15. Relational Operators Return 1 or 0

In C, a relational expression evaluates to an int value: 1 for true and 0 for false.

printf("%d\n", 10 > 5);
printf("%d\n", 10 < 5);

Output:

1
0

16. Relational Operators with Variables

int age = 25;
int limit = 18;

int result = age > limit;

printf("%d", result);

Output:

1

The result can be stored in an integer variable.

17. Using Relational Operators in if

Relational operators are frequently used with if statements.

int age = 20;

if (age >= 18)
{
    printf("Eligible");
}

The message is displayed because the condition is true.

18. Using Relational Operators in if-else

int marks = 35;

if (marks >= 40)
{
    printf("Pass");
}
else
{
    printf("Fail");
}

Output:

Fail

19. Comparing User Input

#include <stdio.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18)
    {
        printf("Adult");
    }

    return 0;
}

The relational operator compares the entered age with 18.

20. Comparing Two Numbers

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    if (a > b)
    {
        printf("A is greater");
    }
    else if (a < b)
    {
        printf("B is greater");
    }
    else
    {
        printf("Both are equal");
    }

    return 0;
}

This program uses three relational comparisons to compare two numbers.

21. Relational Operators with Characters

Characters can also be compared because character constants have integer values based on the execution character set.

char a = 'A';
char b = 'B';

printf("%d", a < b);

The result is commonly 1 in character sets where 'A' has a smaller value than 'B', such as ASCII.

22. Relational Operators with Floating-Point Values

float price = 99.5;

if (price > 50.0)
{
    printf("Price is greater than 50");
}

Relational operators can compare floating-point values as well.

23. Difference Between = and ==

A common beginner mistake is confusing assignment = with equality comparison ==.

int age = 20;

if (age == 20)
{
    printf("Age is 20");
}

Here, = assigns a value, while == compares values.

24. Combining Relational Operators

Relational expressions can be combined with logical operators to create more complex conditions.

int age = 25;
int marks = 70;

if (age >= 18 && marks >= 40)
{
    printf("Condition satisfied");
}

Here, two relational expressions are combined using logical AND.

25. Finding the Largest of Two Numbers

#include <stdio.h>

int main()
{
    int a = 50;
    int b = 30;

    if (a > b)
    {
        printf("A is largest");
    }
    else
    {
        printf("B is largest");
    }

    return 0;
}

The > operator determines which number is larger.

26. Checking Pass or Fail

#include <stdio.h>

int main()
{
    int marks = 55;

    if (marks >= 40)
    {
        printf("Pass");
    }
    else
    {
        printf("Fail");
    }

    return 0;
}

The >= operator checks whether the marks meet the minimum required value.

27. Checking a Number Range

int number = 50;

if (number >= 1 && number <= 100)
{
    printf("Number is between 1 and 100");
}

The relational operators check both boundaries of the range.

28. Common Mistakes

  • Using = instead of == for comparison.
  • Confusing > with >=.
  • Confusing < with <=.
  • Forgetting that a false relational expression produces 0.
  • Forgetting that a true relational expression produces 1.
  • Using comparisons incorrectly inside complex conditions.

29. Complete Relational Operators Program

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter first number: ");
    scanf("%d", &a);

    printf("Enter second number: ");
    scanf("%d", &b);

    printf("Equal: %d\n", a == b);
    printf("Not Equal: %d\n", a != b);
    printf("Greater: %d\n", a > b);
    printf("Less: %d\n", a < b);
    printf("Greater or Equal: %d\n", a >= b);
    printf("Less or Equal: %d\n", a <= b);

    return 0;
}

This program demonstrates all six relational operators.

30. Practice with Relational Operators

Practice the following programs:

  1. Check whether two numbers are equal.
  2. Check whether two numbers are different.
  3. Find the greater number between two numbers.
  4. Find the smaller number between two numbers.
  5. Check whether a person is 18 or older.
  6. Check whether marks are greater than or equal to 40.
  7. Check whether a number is between 1 and 100.
  8. Compare two decimal values.
  9. Compare two character values.
  10. Create a program that compares two numbers using all six relational operators.

Practice these comparisons with if and if-else statements to become comfortable with decision-making in C.

📌 Key Points

  • Relational operators are used to compare values.
  • == checks whether two values are equal.
  • != checks whether two values are different.
  • > checks for greater than.
  • < checks for less than.
  • >= checks for greater than or equal to.
  • <= checks for less than or equal to.
  • A true relational expression produces 1.
  • A false relational expression produces 0.
  • Relational operators are commonly used with if, if-else, and loops.

🧠 Quick Quiz

Question: Which operator is used to check whether two values are equal in C?