Content Strategy Manager
Relational operators are essential building blocks in the C programming language that enable comparisons and decision-making within programs. The Relational Operators in C is an important concept according to the GATE Syllabus for CSE (Computer Science Engineering).
- By comparing two operands, these operators determine the relationship between them, providing true or false results in the form of Boolean values.
- Beyond numerical values, they are versatile tools that can also compare characters and strings based on their ASCII values.
- Understanding the behavior and precedence of relational operators empowers C programmers to make informed choices
- Unlocking the potential for efficient conditional execution and logical operations in their code.
Table of Content |
Key Terms: Relational Operators in C, Equal To,Not Equal To, Less Than, Greater Than, Boolean Value
Types Of Relational Operators In C
[Click Here for Sample Questions]
Relational operators in the C language are used to compare two values to determine the relationship between them. They include:
- Equal to (==)
- Not equal to (!=)
- Less than (<)
- Greater than (>)
- Less than or equal to (<=)
- Greater than or equal to (>=)
These operators allow programmers to perform comparisons and make decisions based on the results of these comparisons in C programs.
Working Of Relational Operators In C
[Click Here for Sample Questions]
This discussion delves into the fundamental concepts of relational operators in the C programming language, which are essential tools for comparing values and understanding their relationships, enabling efficient decision-making and conditional execution in programs.
Equal to Operator (==)
Checks if two operands are equal.
- Returns true if they are equal, false otherwise.
- Example: 3 == 3 evaluates to true, 2 == 3 evaluates to false.
Not equal to Operator (!=)
Checks if two operands are not equal.
- Returns true if they are not equal, false otherwise.
- Example: 3 != 3 evaluates to false, 3 != 4 evaluates to true.
Less than Operator (<)
Compare the first operand to see if it is less than the second operand.
- Returns true if the first operand is less than the second, false otherwise.
- Example: 5 < 4 evaluates to false, 6 < 8 evaluates to true.
Greater than Operator (>)
Compare the first operand to see if it is greater than the second operand.
- Returns true if the first operand is greater than the second, false otherwise.
- Example: 5 > 4 evaluates to true, 7 > 8 evaluates to false.
Less than or equal to Operator (<=)
Checks if the first operand is less than or equal to the second operand.
- Returns true if it is less than or equal, false otherwise.
- Example: 9 <= 9 and 6 <= 9 both evaluate to true, 9 <= 8 evaluates to false.
Greater than or equal to Operator (>=)
Checks if the first operand is greater than or equal to the second operand.
- Returns true if it is greater than or equal, false otherwise.
- Example: 2 >= 2 and 4 >= 2 both evaluate to true, 3 >= 4 evaluates to false.
Summary of Working of Relational Operators In C
[Click Here for Sample Questions]
The table provides a concise overview of relational operators and their corresponding evaluations in the C programming language.It demonstrates how each operator checks for equality, inequality, less than, greater than, less than or equal to, and greater than or equal to conditions between two operands, and indicates the resulting Boolean value based on the evaluations in C.
Meaning | Operator | Details |
---|---|---|
Equal to | == | (P == Q) is not true 7 == 9 gets evaluated to 0 7 == 7 gets evaluated to 1 |
Not equal to | != | (P != Q) is true 7 != 9 gets evaluated to 1 7 != 7 gets evaluated to 0 |
Less than | < | (P < Q) is not true 7 < 5 gets evaluated to 0 7 < 9 gets evaluated to 1 |
Greater than | > | (P > Q) is true 7 > 5 gets evaluated to 1 7 > 9 gets evaluated to 0 |
Less than or equal to | <= | (P <= Q) is not true 7 <= 5 gets evaluated to 0 7 <= 9 gets evaluated to 1 7 <= 7 gets evaluated to 1 |
Greater than or equal to | >= | (P >= Q) is true 7 >= 5 gets evaluated to 1 7 >= 9 gets evaluated to 0 7 >= 7 gets evaluated to 1 |
Also Read:
Related Links | ||
---|---|---|
Pipeline Hazards | Primary Memory | Ring Topology |
Recursive Function in C | Process in Operating System | Secondary Memory |
PnP Full Form | Transitive Dependency in DBMS | Tree Topology |
Example Of Relational Operators In C
[Click Here for Sample Questions]
The example is given below:
Example Of Relational Operators In C
After compiling and executing the above program, it produces the following result −
- Line 1 - x is not equal to y
- Line 2 - x is not less than y
- Line 3 - x is greater than y
- Line 4 - x is either less than or equal to y
- Line 5 - y is either greater than or equal to x
Things to Remember
- Relational operators in C are the type of operators used to compare two operands.
- The return type of all the relational operators in the C language is a boolean value.
- Relational operators in C are also used to compare characters and strings based on their ASCII value.
- Relational operators in C follow the precedence rules while determining the result.
- They can be used for comparisons involving numerical values, characters, and strings.
- When multiple relational operators are used in a single expression
- C follows the precedence and associativity rules to evaluate the expression.
Sample Questions
Ques: In the given C code, what will be the value stored in the "result" variable after execution? (1 mark)
Ans: The value stored in the "result" variable after execution will be 1.
Ques: What will be the value of the "result" variable in the following C code? (2 marks)
Ans: The value of the "result" variable will be 9.
Ques: In the given C code, what will be the output when comparing the characters 'c' and 'a'? (2 marks)
Ans: The output will be "ch1 is greater than ch2".
Ques: What will be the output when comparing the characters 'A' and 'a' in the following C code? (2 marks)
Ans: The output will be "ch1 is smaller than ch2".
Ques: What is the difference between the greater than/less than operator and the greater than and equal to/less than or equal to operator? (3 marks)
Ans: Greater Than/Less Than Operator (<, >): The greater than operator checks if the first available operand is greater than the second one, while the less than operator checks if the first available operand is less than the second one.
Greater Than or Equal To/Less Than or Equal To Operator (>=, <=): The greater than or equal to operator checks if the first available operand is equal to or greater than the second one, while the less than or equal to operator checks if the first available operand is equal to or less than the second one.
Examples:
- Greater Than Operator: 5 < 6 is true, 5 < 5 is false.
- Less Than Operator: 5 > 4 is true, 5 > 5 is false.
- Greater Than or Equal To Operator: 5 <= 6 is true, 5 <= 5 is true.
- Less Than or Equal To Operator: 5 >= 4 is true, 5 >= 5 is true.
Ques: Does the equal to operator work with a single = sign? (2 marks)
Ans: No, the equal to operator does not work with a single = sign. You must use the double equal to signs (==) for the equal to operator to function correctly.
Example:
- Incorrect: if (x = 5) // This is assignment, not comparison.
- Correct: if (x == 5) // This is a comparison for equality.
By using a combination of bullet points and paragraphs, the questions become more organised and easier to follow, providing clear explanations and examples for each concept.
For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates
Check-Out:
Comments