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.
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 provides the following relational operators:
== (Equal to)!= (Not equal to)< (Less than)> (Greater than)<= (Less than or equal to)>= (Greater than or equal to)Let's understand these operators with the help of some practical examples.
#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.
#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.
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.
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!