C language – Arithmetic and Relational Operators in C

operators in c programming

In C language an operator is a symbol that specifies to be performed the operand. The items that operators act upon are called operands. Some operators require two operands called binary operators, while other acts upon only one operand called unary operator.

The operators usually form a part of mathematical or logical expressions. A c programming language provides several operators to perform different kinds of operations.

The symbol which is used to perform logical and mathematical operations in a C program is called C operators.

Example: a + b

Operators in c language

Where ‘+’ is Operator and ‘a’ and ‘b’ are the operands. Consider the expression a + b * 2. where +, * are operators a, b are variables, 2 is constant and a + b * 2 is an expression.

The operators tell the computer to perform the specified operation on operands and these are used in programs to manipulate data and variables.

Types of operators in C

  • Arithmetic operators.
  • Relational operators.
  • Logical operators.
  • Assignment operators.
  • Increment and Decrement operators.
  • Ternary operator.
  • Bitwise operators.
  • Special operators.

 

Arithmetic Operators

A c programming language allows us to carry out basic arithmetic operations like subtraction, multiplication, and division. The following table shows the Arithmetic Operators and their meaning.

OperatorMeaningExamples
+Addition3 + 4 = 7
Substraction7 – 2 = 5
*Multiplication3 * 8 = 24
/Division9/3 = 3
%Modulo division9 % 2 =1

All the above are called ‘Binary’ operators as they act upon two operands at a time.

[Note: The modulo division gives the remainder of an integer division.]

Example:

main()
{
   int a = 10, b = 3, c;
   c = a % b;
   printf("%d", c);
}

Output
1

Here the remainder value only is assigned to variable ‘c’. The operation ‘%’ cannot be used with real operands.

Arithmetic operators can be classified as

⊗  Unary arithmetic – It requires only one operand

Example: +x, -y

⊗  Binary arithmetic – It requires two operands

Example: a+b, a-b, a/b, a*b, a%b

⊗  Integer arithmetic – It requires both operands are integer values for arithmetic operation.

Example: a = 5, b = 4

        a +b is 9 and a – b is 1

⊗  Floating Point Arithmetic: It requires both operands are float types for arithmetic operation.

Example: a= 6.5, b = 3.5

        a + b is 10.0 and a – b is 3.0

Example Program

Program to find out all arithmetic operations on two given values

Output: 
The values are:
sum = 8, sub = -4, mul = 12, div = 0.000000 
Remainder of division of b & d is 2 a = 0

Explanation: This program performs all the arithmetic operations such as addition, subtraction, division, multiplication, and modulo division on two operands that are inputted by the user.

Relational operators in c language

In C language relational operators are used to compare two or more operands. Operators may be variables, constants, or expressions.

For example, we may compare the price of two Laptops, or the grade of two students, and so on. These comparisons can be done with the use of relational operators.

The following table shows the Relational Operators.

OperatorMeaningExampleReturn value
<is less than2<91
<=is less than or equal to2<=1
>is greater than2>90
>=is greater than or equal3>=21
==is equal2 == 30
!=is not equal to2 != 20

Syntax: AE1 relational operator AE2

Description: AE1 and AE2 are constants or expresssion variables.

Example: 9 > 2 yields True, which is equal to 1.


[Note: When arithmetic expressions are used on either side of a relational operator, the arithmetic expression will be evaluated first and then the result compared.]

Suppose the ‘a’, ‘b’ and ‘c’ are integer variables, whose values are 1,2,3 respectively.

a = 1, b = 2, c = 3
ExpressionInterpretationvalue
a<bTrue1
(a + b) >= cTrue1
(b + c) > (a + 5)False0
c != 3False0
b == 2True1

The value of the relational expression is either one or zero.

Relational operators are used in the decision-making process. They are generally used in conditional and control statements.

Example program

Program to use various relational operators and display their return values.

Output

Conditon : Return values
7 != 7   :      0
7 == 7   :      1
7 >= 7   :      1
7 <= 7   :      1
7 != 5   :      1

 

Explanation: In the above program, the true conditions return 1, and false conditions return 0. In this example, the first condition is false and the remaining conditions are true.

Hence return value for the first statement is 0 and the return value for all the remaining statements is 1.