C Relational Operators 🎯

beginner
13 min

C Relational Operators 🎯

Welcome to the exciting world of C programming! In this lesson, we'll dive deep into C Relational Operators. These operators help compare values and are essential for decision making in your programs.

What are Relational Operators? 📝

Relational operators are used to compare two operands and return a boolean value (true or false). They are fundamental to C programming and help in making decisions within your code.

C Relational Operators 💡

C provides the following relational operators:

  1. == (Equal to)
  2. != (Not equal to)
  3. < (Less than)
  4. > (Greater than)
  5. <= (Less than or equal to)
  6. >= (Greater than or equal to)

Examples 💻

Let's understand these operators with the help of some practical examples.

Example 1: Checking Equality

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

In this example, we compare two integers a and b. Since they are not equal, the output is 0, which is false in a boolean context.

Example 2: Comparing Integers

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

In this example, we compare a and b to check if a is less than b. Since it is, the output is 1, which is true in a boolean context.

Quiz 💡

Question: What does the != operator do? A: Checks if the operands are equal B: Checks if the operands are not equal C: Checks if the operand is greater than Correct: B Explanation: The != operator checks if the operands are not equal.

Practical Applications ✅

Relational operators are used extensively in programming for conditional statements, loops, and decision making. They help you create more dynamic and responsive code.

Stay tuned for our next lesson on C Logical Operators!