- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
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
- 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)
- 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
- 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");
}
- Loops: Used to control the execution of loops.
C
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
Points to Remember
- Boolean Return:
- Relational operators return 1 for true and 0 for false.
C
int result = (10 > 5); // result = 1
- Type Compatibility:
- Ensure operands being compared are of compatible data types to avoid unexpected behavior.
- 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");
}
- 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.
