C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we'll explore relational operators in the C programming language. Relational operators are used to compare two values and evaluate to a boolean result (true or false). We'll cover the basic syntax and provide some examples to demonstrate their use.
Relational Operators
There are six relational operators in C:
>
: greater than<
: less than>=
: greater than or equal to<=
: less than or equal to==
: equal to!=
: not equal toRelational operators have lower precedence than arithmetic operators, so they're evaluated after any arithmetic operations in an expression. The result of a comparison using relational operators is either 0 (false) or 1 (true).
Examples
Here are some examples to demonstrate the use of relational operators in C:
#include <stdio.h> int main() { int a = 5, b = 10; printf("a > b: %d\n", a > b); // Output: 0 (false) printf("a < b: %d\n", a < b); // Output: 1 (true) printf("a >= b: %d\n", a >= b); // Output: 0 (false) printf("a <= b: %d\n", a <= b); // Output: 1 (true) printf("a == b: %d\n", a == b); // Output: 0 (false) printf("a != b: %d\n", a != b); // Output: 1 (true) return 0; }
Using Relational Operators in Conditional Statements
Relational operators are commonly used in conditional statements, like if
, if-else
, and switch
. Here's an example that demonstrates using relational operators in if
statements:
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) { printf("The number is positive.\n"); } else if (num < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); } return 0; }
In this example, the user enters a number, and the program uses relational operators to determine whether it's positive, negative, or zero.
Using Relational Operators in Loops
Relational operators can also be used as loop conditions, like in for
and while
loops. Here's an example that demonstrates using a relational operator in a for
loop:
#include <stdio.h> int main() { int n, i; printf("Enter an integer: "); scanf("%d", &n); printf("Multiplication table for %d:\n", n); for (i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } return 0; }
In this example, the user enters an integer, and the program uses a for
loop with a relational operator (i.e., i <= 10
) to print the multiplication table for that integer.
Conclusion
In this tutorial, we learned about relational operators in the C programming language, their basic syntax, and usage. Relational operators are fundamental tools for making comparisons and controlling the flow of your program using conditional statements and loops.
Comparing integers and floating-point numbers using relational operators:
#include <stdio.h> int main() { int num1 = 5; int num2 = 10; // Comparing integers if (num1 < num2) { printf("%d is less than %d\n", num1, num2); } float floatNum1 = 3.14; float floatNum2 = 2.5; // Comparing floating-point numbers if (floatNum1 >= floatNum2) { printf("%.2f is greater than or equal to %.2f\n", floatNum1, floatNum2); } return 0; }
Chaining and combining relational operators in C programming:
#include <stdio.h> int main() { int x = 5; int y = 10; int z = 7; // Chaining relational operators if (x < y && y > z) { printf("%d is less than %d and %d is greater than %d\n", x, y, y, z); } // Combining relational operators if (x != y || y == z) { printf("%d is not equal to %d or %d is equal to %d\n", x, y, y, z); } return 0; }
C code examples demonstrating the use of relational operators:
#include <stdio.h> int main() { int a = 10; int b = 20; // Using relational operators in expressions int result = (a > b) ? a : b; printf("The larger value is: %d\n", result); return 0; }
Boolean expressions and truth values with relational operators in C:
1
for true, 0
for false), and these values can be used in boolean expressions.#include <stdio.h> int main() { int age = 25; // Using relational operators in a boolean expression int isAdult = (age >= 18); if (isAdult) { printf("You are an adult.\n"); } else { printf("You are not yet an adult.\n"); } return 0; }
Common mistakes and pitfalls with relational operators in C:
=
instead of ==
for equality comparison and assuming floating-point precision for exact equality.#include <stdio.h> int main() { int x = 5; int y = 10; // Common mistake: using '=' instead of '==' if (x = y) { printf("This will always be true!\n"); } float a = 0.1; float b = 0.1 + 0.2; // Common pitfall with floating-point precision if (a == b) { printf("This may not be true due to floating-point precision.\n"); } return 0; }
Relational operators in conditional statements and loops in C:
#include <stdio.h> int main() { int number = 15; // Using relational operators in a conditional statement if (number > 10) { printf("%d is greater than 10.\n", number); } else { printf("%d is not greater than 10.\n", number); } // Using relational operators in a loop condition for (int i = 0; i < 5; i++) { printf("Iteration %d\n", i); } return 0; }