- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Constants
- C - Literals
- C - Escape sequences
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- 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
- C - Functions
- C - Main Functions
- C - Return Statement
- C - Recursion
- C - Scope Rules
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointer Arithmetics
- C - Passing Pointers to Functions
- C - Strings
- C - Array of Strings
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Pointers to Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Relational Operators in C
The relational operators in C are the symbols that are defined to perform comparison of two values. The familiar angular brackets < and > are the relational operators in addition to few more as listed in a table below. These relational operators are used in a Boolean expression. All the relational operators evaluate to either true or false. C doesn’t have a Boolean data type. Instead, 0 is interpreted as false and any nonzero result is treated as true.
Here is a simple example of relational operator in C −
Example
#include <stdio.h> int main() { int op1 = 5; int op2 = 3; printf("op1: %d op2: %d op1<op2: %d\n", op1, op2, op1<op2); return 0; }
Output
op1: 5 op2: 3 op1<op2: 0
The relational operators have an important role to play in decision control and looping statements in C.
The following table lists all the relational operators in C −
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) |
!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) |
> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) |
< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) |
>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) |
<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) |
All the relational operators are binary operators. Since they perform comparison, they need two operands on their either side.
The = symbol is defined as the assignment operator in C. Hence, C uses == (double equal) as equality operator. The angular brackets > and < are defined as greater than and less than operators. When combined with = symbol, they form >= operator for greater than or equal, and <= operator for less than or equal comparison. Finally, the = symbol prefixed with !, i.e. != symbol is used as inequality operator.
Following example shows all the relational operators in use.
Example
#include <stdio.h> int main() { int a = 21; int b = 10; int c ; printf("a: %d b: %d\n", a,b); if( a == b ) { printf("Line 1 - a is equal to b\n" ); } else { printf("Line 1 - a is not equal to b\n" ); } if ( a < b ) { printf("Line 2 - a is less than b\n" ); } else { printf("Line 2 - a is not less than b\n" ); } if ( a > b ) { printf("Line 3 - a is greater than b\n" ); } else { printf("Line 3 - a is not greater than b\n" ); } /* Lets change value of a and b */ a = 5; b = 20; printf("a: %d b: %d\n", a,b); if ( a <= b ) { printf("Line 4 - a is either less than or equal to b\n" ); } if ( b >= a ) { printf("Line 5 - b is either greater than or equal to b\n" ); } if( a != b ) { printf("Line 6 - a is not equal to b\n" ); } else { printf("Line 6 - a is equal to b\n" ); } return 0; }
Output
a: 21 b: 10 Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b a: 5 b: 20 Line 4 - a is either less than or equal to b Line 5 - b is either greater than or equal to b Line 6 - a is not equal to b
The == operator needs to be used with care. Remember that = (single equal) is the assignment operator in C. If used by mistake in place of equality operator, you get incorrect output as follows −
Example
#include <stdio.h> int main() { int a = 5; int b = 3; if (a=b) { printf("a is equal to b"); } else { printf("a is not equal to b"); } return 0; }
Output
a is equal to b
In fact, the value of b is assigned to a here, which is non-zero, and hence the if expression becomes true.
The char types can also be used as the operand for all the relational operators, as a char type is a subset of int type.
Example
#include <stdio.h> int main() { char a = 'B'; char b = 'd'; printf("a: %c b: %c\n", a,b); if( a == b ) { printf("Line 1 - a is equal to b\n" ); } else { printf("Line 1 - a is not equal to b\n" ); } if ( a < b ) { printf("Line 2 - a is less than b\n" ); } else { printf("Line 2 - a is not less than b\n" ); } if ( a > b ) { printf("Line 3 - a is greater than b\n" ); } else { printf("Line 3 - a is not greater than b\n" ); } if( a != b ) { printf("Line 4 - a is not equal to b\n" ); } else { printf("Line 4 - a is equal to b\n" ); } return 0; }
Output
a: B b: d Line 1 - a is not equal to b Line 2 - a is less than b Line 3 - a is not greater than b Line 4 - a is not equal to b
However, the relational operators cannot be used for comparing secondary types such as array, or derived types such as struct or union types.