C Programs Tutorials | IT Developer
IT Developer

C Programming - C Relational Operators



Share with a Friend

C Programming - C Relational Operators

C Relational Operators

Relational operators in C are used to compare two values or expressions. These operators return a boolean value: 1 (true) if the condition is satisfied or 0 (false) otherwise.

List of Relational Operators

Operator Description Example Result

==

Equal to

a == b

True if a is equal to b

!=

Not equal to

a != b

True if a is not equal to b

Less than

a < b

True if a is less than b

Greater than

a > b

True if a is greater than b

<=

Less than or equal to

a <= b

True if a is less than or equal to b

>=

Greater than or equal to

a >= b

True if a is greater than or equal to b

Key Characteristics

  1. Equality Operators:
    • == checks if two values are equal.
    • != checks if two values are not equal.

C

int a = 5, b = 3;

printf("%d\n", a == b); // Output: 0 (false)

printf("%d\n", a != b); // Output: 1 (true)

  1. Comparison Operators:
    • <, >, <=, and >= compare the relative magnitude of two values.

C

int a = 5, b = 3;

printf("%d\n", a > b);  // Output: 1 (true)

printf("%d\n", a < b);  // Output: 0 (false)

printf("%d\n", a >= 5); // Output: 1 (true)

printf("%d\n", b <= 3); // Output: 1 (true)

Example Program

C

#include <stdio.h>

int main() {

    int a = 10, b = 20;

    printf("a == b: %d\n", a == b);  // Check if a is equal to b

    printf("a != b: %d\n", a != b);  // Check if a is not equal to b

    printf("a < b: %d\n", a < b);    // Check if a is less than b

    printf("a > b: %d\n", a > b);    // Check if a is greater than b

    printf("a <= b: %d\n", a <= b);  // Check if a is less than or equal to b

    printf("a >= b: %d\n", a >= b);  // Check if a is greater than or equal to b

    return 0;

}

Output:

a == b: 0

a != b: 1

a < b: 1

a > b: 0

a <= b: 1

a >= b: 0

Operator Precedence

Relational operators have a lower precedence than arithmetic operators but a higher precedence than logical operators.

Precedence Level Operators Associativity

Higher

+, -, *, /

Left-to-right

Middle

<, >, <=, >=

Left-to-right

Lower

&&, `

 

Common Usage

  1. Conditional Statements: Relational operators are widely used in if, else if, and while statements.

C

int a = 10, b = 20;

if (a < b) {

    printf("a is less than b\n");

}

  1. Loops: Used to control the execution of loops.

C

for (int i = 0; i < 10; i++) {

    printf("%d\n", i);

}

Points to Remember

  1. Boolean Return:
    • Relational operators return 1 for true and 0 for false.

C

int result = (10 > 5); // result = 1

  1. Type Compatibility:
    • Ensure operands being compared are of compatible data types to avoid unexpected behavior.
  2. Floating-Point Comparisons:
    • Be cautious when comparing floating-point numbers due to potential precision issues.

C

float a = 0.1, b = 0.1 + 0.1 + 0.1 - 0.3;

if (a == b) {

    printf("Equal");

} else {

    printf("Not Equal");

}

  1. Chaining Comparisons:
    • In C, relational operators cannot be chained directly.

C

int a = 5, b = 10, c = 15;

if (a < b < c) {  // Invalid comparison

    printf("Chained comparison");

}

Relational operators form the foundation of decision-making and control structures in C, enabling programmers to compare and evaluate expressions effectively.