โž• C Operators
Estimated reading: 3 minutes 8 views

๐Ÿ“Š C Relational Operators โ€“ Compare Values in C Programs

๐Ÿงฒ Introduction โ€“ What Are Relational Operators in C?

Relational operators in C are used to compare two values or expressions. These comparisons evaluate to either true (1) or false (0) and are essential for building control structures such as if, while, and for loops. They form the foundation of decision-making in C programming.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The list of relational operators in C
  • How to compare variables and expressions
  • Use of relational operators in conditional statements
  • Examples and common comparison logic

๐Ÿ“š List of Relational Operators

OperatorDescriptionExample (a = 10, b = 5)Result
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater than or equal toa >= b1 (true)
<=Less than or equal toa <= b0 (false)

๐Ÿงช Using Relational Operators in Code

#include <stdio.h>
int main() {
    int a = 10, b = 5;
    
    printf("a == b: %d\n", a == b);
    printf("a != b: %d\n", a != b);
    printf("a > b: %d\n", a > b);
    printf("a < b: %d\n", a < b);
    printf("a >= b: %d\n", a >= b);
    printf("a <= b: %d\n", a <= b);
    return 0;
}

Output:

a == b: 0  
a != b: 1  
a > b: 1  
a < b: 0  
a >= b: 1  
a <= b: 0  

๐Ÿ” Relational Operators in Conditional Statements

Relational operators are commonly used inside if, while, and for loops to control program flow:

if (score >= 50) {
    printf("Pass\n");
} else {
    printf("Fail\n");
}

They can also be used with logical operators (&&, ||, !) for compound conditions.


โš ๏ธ Notes on Relational Operators

  • The result of any relational operation is either 1 (true) or 0 (false).
  • Always use == for comparison, not = (assignment).
  • Comparisons are type-safeโ€”comparing int with float will work via implicit conversion.

โœ… Example:

int x = 5;
float y = 5.0;
printf("%d\n", x == y);  // Output: 1 (true)

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Relational operators in C are critical for building decision-making structures. They allow comparison of values, variables, or expressions and are typically used in branching and looping constructs.

๐Ÿ” Key Takeaways:

  • Relational operators return 1 for true, 0 for false.
  • Used heavily in if, while, for, and switch logic.
  • Must not confuse == (comparison) with = (assignment).
  • Can compare integers, characters, floats, and mixed types.

โš™๏ธ Real-World Relevance:

Used in grading systems, login checks, limit checks, authentication, and any application that needs conditional decision-making.


โ“ Frequently Asked Questions (FAQ)

โ“ What is the difference between == and = in C?

โœ… == checks for equality.
โœ… = assigns a value.

if (a == b) { ... }  // Correct  
if (a = b) { ... }   // โŒ Logic error

โ“ Can I use relational operators with characters?

โœ… Yes. Characters are internally treated as integers using ASCII values.

if ('A' < 'Z')  // true

โ“ Do relational operators work with floats?

โœ… Yes. You can compare float and double values.

float a = 3.14;
if (a > 2.0) { ... }

โ“ Can relational operators be combined?

โœ… Yes. Combine with logical operators:

if (score >= 50 && score <= 100) { ... }

โ“ What does != mean?

โœ… != means “not equal to”. It returns true (1) if the two operands are different.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“Š C Relational Operators

Or Copy Link

CONTENTS
Scroll to Top