C language content

Relational operators in the C programming language are used to compare two values. They yield a boolean result, which is either true (non-zero) or false (zero). These operators are crucial for decision-making in programming, allowing the execution of different code paths based on the comparison of values.

Here’s a detailed description of each relational operator in C:

1. Equal to ( ' == ' )

The ‘==‘operator checks if two operands are equal. If they are, the result is ‘true‘ (1), otherwise, it is ‘false' (0).

Example:

				
					int a = 5, b = 5;
int result = (a == b);  // result is 1 (true)

int c = 5, d = 3;
int result2 = (c == d);  // result is 0 (false)

				
			

2. Not equal to ( ' != ' )

The ‘!=‘ operator checks if two operands are not equal. If they are not equal, the result is ‘true‘ (1), otherwise, it is ‘false‘ (0).

Example:

				
					int a = 5, b = 3;
int result = (a != b);  // result is 1 (true)

int c = 5, d = 5;
int result2 = (c != d);  // result is 0 (false)

				
			

3. Greater than ( ' > ' )

The ‘>‘ operator checks if the left operand is greater than the right operand. If it is, the result is ‘true‘ (1), otherwise, it is ‘false‘ (0).

Example:

				
					int a = 5, b = 3;
int result = (a > b);  // result is 1 (true)

int c = 3, d = 5;
int result2 = (c > d);  // result is 0 (false)

				
			

4. Less than ( ' < ' )

The < operator checks if the left operand is less than the right operand. If it is, the result is ‘true‘ (1), otherwise, it is ‘false‘ (0).

Example:

				
					int a = 3, b = 5;
int result = (a < b);  // result is 1 (true)

int c = 5, d = 3;
int result2 = (c < d);  // result is 0 (false)

				
			

5. Greater than or equal to ( ' >= ' )

The ‘>=‘ operator checks if the left operand is greater than or equal to the right operand. If it is, the result is ‘true‘ (1), otherwise, it is ‘false‘ (0).

Example:

				
					int a = 5, b = 5;
int result = (a >= b);  // result is 1 (true)

int c = 5, d = 3;
int result2 = (c >= d);  // result is 1 (true)

int e = 3, f = 5;
int result3 = (e >= f);  // result is 0 (false)

				
			

6. Less than or equal to ( ' <= ' )

The ‘<=‘ operator checks if the left operand is less than or equal to the right operand. If it is, the result is ‘true‘ (1), otherwise, it is ‘false‘ (0).

Example:

				
					int a = 3, b = 5;
int result = (a <= b);  // result is 1 (true)

int c = 5, d = 5;
int result2 = (c <= d);  // result is 1 (true)

int e = 5, f = 3;
int result3 = (e <= f);  // result is 0 (false)

				
			

Detailed Examples and Notes

Relational Operators with Different Data Types

Relational operators can be used with different data types, such as integers and floating-point numbers. When operands of different types are compared, implicit type conversion occurs to compare them.

Example:

				
					int a = 5;
float b = 5.0;
int result = (a == b);  // result is 1 (true) because 5 is equal to 5.0

double c = 5.0;
float d = 5.1;
int result2 = (c < d);  // result is 1 (true) because 5.0 is less than 5.1

				
			

Using Relational Operators in Conditional Statements

Relational operators are often used in conditional statements to control the flow of the program based on comparisons.

Example:

				
					int a = 5, b = 3;
if (a > b) {
    printf("a is greater than b\n");
} else {
    printf("a is not greater than b\n");
}

int c = 3, d = 3;
if (c <= d) {
    printf("c is less than or equal to d\n");
} else {
    printf("c is greater than d\n");
}

				
			

Combining Relational Operators with Logical Operators

Relational operators are frequently used together with logical operators (‘&&‘, ‘||‘, ‘!‘) to form more complex conditions.

Example:

				
					int a = 5, b = 3, c = 8;
if ((a > b) && (c > a)) {
    printf("a is greater than b and c is greater than a\n");
}

int x = 5, y = 7;
if ((x < y) || (x > 10)) {
    printf("x is less than y or x is greater than 10\n");
}

				
			

Understanding relational operators and how they interact with various data types and logical operators is essential for effective decision-making and control flow in C programming.

Scroll to Top