# Relational and Equality Operators

by Dinesh Thakur Category: Operator

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

## 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.

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. 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.

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). 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. Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles