• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Operator » Relational and Equality Operators
Next →
← Prev

Relational and Equality Operators

By Dinesh Thakur

The C language provides four relational and two equality operators for comparing the values of expressions. The relational operators are less than (<), greater than (>), less than or equal to (<=) and greater than or equal to (>= ). The equality operators are equal to (==) and not equal to ( ! =). These operators are binary infix operators, i. e., they are used in the form a op b, where a and bare operands (constants, variables or expressions) and op is a relational or an equality operator. Table summarizes these operators.

Note that the token for equality operator is ==. A beginner often makes the mistake of writing the equal to operator simply as =, which is an assignment operator. Thus, a == b means the values of variables a and b are compared for equality, whereas a = b means the value of variable b is assigned to variable a.

                            Table Relational and equality operators in the C language

Category

Operator

Meaning

Example

Associativity

        Relational

< 

Less than

a < b

      Left to right

> 

Greater than

a > b

<=

Less than or equal to

a <= b

>=

Greater than or equal to

a >= b

        Equality      

==

Equal to

a == b

!=

Not equal to

a != b

We’ll be covering the following topics in this tutorial:

  • Relational and Equality Expressions
  • Example Relational and Equality Expressions
  • Evaluation of Relational and Equality Expressions
  • Example Evaluation of Relational and Equality Expressions

Relational and Equality Expressions

An expression containing relational operators is termed a relational expression, whereas an expression containing equality operators is termed an equality expression. A simple relational or equality expression takes the following form:

expr1 op expr2

where expr1 and expr2 are arithmetic expressions and op is a relational or equality operator. An expression may contain more than one relational or equality operator, as in

exprl opt expr2 op2 expr3…..

and may also contain balanced parentheses. The value of a relational or equality expression is of type int. It is equal to 1 if the expression evaluates as true and zero otherwise. We can assign this value to a variable (usually of type int).

Note that an arithmetic expression can also be considered as a relational expression. An expression whose value is zero is considered as false, whereas an expression with a nonzero value is considered as true.

Example Relational and Equality Expressions

Assume that the symbolic constants and variables are defined as follows:

#define PI 3.1415927

int i = 1, j = 2;

double x = 5.0, y = 10.0;

char c = ‘A’, d = ‘a’;

a) Several examples of simple relational and equality expressions are given below.

simple relational and equality expressions

The expressions in the left, middle and right column contain operands of type int, double and char, respectively. The expressions on the first two lines contain relational operators, whereas those on the last line contain equality operators. The operands in these expressions are variables, constants or symbolic constants. The spaces between operators and operands are optional. Thus, the expression i < j can also be written as i<j . However, it is good programming practice to include spaces between operands and operators.

Note that the comparison of operands of type char is done considering their values in the machine’s character set (ASCII assumed). The ASCII value of character ‘A’ is 65. Thus, the value of expression c >= ‘A’ is 1 as this expression means 65 >= 65, which is true.

b) It is not always necessary to know the exact ASCII values of characters, particularly when comparing letters and digits. Just remember that ASCII values of digits are less than the ASCII values of uppercase letters which in turn are less than those of lowercase letters. Also, the digits (o … 9), uppercase letters (A … z) and lowercase letters (a … z) are assigned consecutive ASCII values. .

It is also possible to compare operands of different arithmetic types. Several examples of such mixed-mode relational and equality expressions are given below along with their values.

mixed-mode relational and equality expressions

As the value of the newline character (‘ \n ‘) is 10 (ASCII), expression j == ‘\n’ is false and has the value 0. Also, expression c == 65 is true as the value of variable c is ‘A’, i.e., 65. Finally note that operator tokens must be written exactly as shown in Table. Consider the following relational expressions: i < = j, x = = j, x => 5, y = s. The first three expressions are invalid as the operators are written incorrectly (note the spaces in operators < = and = =). However, the last expression, y = s is a valid assignment expression. It assigns the value 5 to variable y. As the value of this expression is 5 (non-zero), it is considered as true and not false as you might think.

c) Several mathematical equations are given below along with equivalent C expressions.

                     mathematical equations equivalent C expressions

As arithmetic operators have higher precedence than the relational and equality operators, the operands of the latter (which are arithmetic expressions) need not be enclosed in parentheses. However, we can use such parentheses to improve the clarity of the expression, as in (b*b-4*a*c) == 0.

Evaluation of Relational and Equality Expressions

Table summarizes the precedence and associativity rules of relational and equality operators along with arithmetic and assignment operators. The arithmetic operators (unary, multiplicative and additive) have higher precedence than relational operators, which in turn have higher precedence than equality operators. The assignment operators have the lowest precedence amongst these operators. Thus, while evaluating an expression, arithmetic operators are bound to operands first followed by relational, equality and assignment operators.

Table Precedence and associativity of arithmetic, relational, equality and assignment operators

Operator Group

Operators

Associativity

Unary

+ – ++ —

Right to left

Multiplicative

* I %

Left to right

Additive

+ –

Relational

< > <= >=

Equality

== !=

Assignment

= += -= *= /= %=

Right to left

All relational operators (< > <= >=) have equal precedence. Also, both the equality operators (== !=) have equal precedence. Note that the relational and equality operators have left-to-right associativity. Thus, if an expression contains two or more relational operators, they are bound to operands from left to right. The same is true with equality operators.

If an expression contains balanced parentheses, operators within the parentheses are bound first to their operands in the order of their precedence. Also, if an expression contains nested parentheses, the operators in the innermost parentheses are bound first to their operands. When all operators in parenthesized sub-expressions are bound to their operands, the remaining operators in an expression are considered.

Example Evaluation of Relational and Equality Expressions

Assume that variables are declared and initialized as follows:

float a= 1.0, b = 2.0, x = 3.0, y = 5.0;

Consider expression a + b < x * y, in which the multiplication operator has highest precedence, followed by the addition operator. These operations are first bound to their operands and then the values of a+b and x*y are compared. This is depicted in Fig. 5.l. The order in which operators are bound to operands is shown in Fig and expression evaluation is shown in Fig. The expression is correctly interpreted as (a + b) <xy and evaluates as true, i. e., 1 for the values given above. We may use parentheses in this expression to improve the readability, as in (a+ b) < (x * y). 

      Evaluation of relational expressions

Now consider the assignment statement given below in which the variable test is assumed to be of type int. ·

test = a + b < x * y;

As the assignment operator has least precedence in this statement, the variable test is assigned value of relational expression a + b < x * y, i. e., 1.

Next, consider the evaluation of expression a <= b >= x. Since the relational operators have left-to-right associativity, the <= operator is bound first followed by the >= operator as shown in Fig. Thus, the expression is equivalent to (a <= b) >= x. The expression a <= x evaluates as true, i. e., 1. Now the given expression, which is equivalent to 1 >= x, evaluates as false, i. e., 0.

The evaluation of a more involved relational expression, (a + b) /5 == x / (2 * (y + 3 )) , is considered next. The order in which the operands are bound to operators is shown in Fig. It can be easily verified that for the values of the variables given above, this expression evaluates as false, i. e., 0.

Finally, expression a > 3 != b < 5. 0, containing a relational as well as an equality operator is considered. Since the relational operators (< and >) have higher precedence than the equality operator ( ! =), they are first bound to their operands in the left-to-right order. Then the ! = operator is bound to its operands, i. e., the expressions a > 3 and b < 5.0as shown in Fig. It can be easily verified that the given expression evaluates as true, i. e., 1.

You’ll also like:

  1. Write A C++ Program To Comparing Integers Using If Statements, Relational Operators And Equality Operators.
  2. Types of Relational Operators
  3. Relational Operators in Java Example
  4. Explain purpose of relational operators and logical operator
  5. Write C++ program illustrates the hierarchy rule in a Boolean expression involving arithmetic, relational and logical operators
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW