Arithmetic Operators

To perform arithmetic operations such as addition, subtraction, multiplication and division, C language provides you with arithmetic operators such as +, -, * and / respectively. These arithmetic operations can be performed on integer and floating-point data types.

c = a + b;

In the above statement, there are 2 operations that are happening. First operation is arithmetic operation, addition of a and b, second operation is assignment operation, sum of a and b is assigned to c.

Every operation comprises an operator and 2 operands. Operator is the symbol that signify the operation, and operand is the variable or constant upon which the operation is performed. In the above example, a and b are the operands and + is the operator.

Basic Arithmetic Operators

These are the operators we are very much familiar with in our day to day activities.

OperationOperatorExample
Addition+2 + 2
Subtraction5 – 2
Multiplication*2 * 2
Division/8 / 2

Arithmetic Operator Precedence

If an expression consists of multiple operations, the order of evaluation of those operations would be as below:

OrderOperators
1* / %
2+ –
3=

What would be the output of the below program?

#include <stdio.h>

void main()
{
    int n = 2 + 8 / 2 * 2 * 5 % 2;
    printf("%d", n);
}

In this expression, multiplication, division and modulus operations will be executed as per their order in the expression. First 8/2 evaluates to 4, then 4*2*5 evaluates to 40, then 40%2 evaluates to 0, finally 2 + 0 evaluates to 2.

Modulus Operator

Apart from the basic arithmetic operators, we also have a modulus operator that only work with integer type variables and constants. This operator can be used for finding the reminder of a division. This operator is also called reminder operator.

#include <stdio.h>

void main()
{
    int a = 5, b = 2;
    int c = a % b;
    printf("Reminder of %d / %d is %d", a, b, c);
}

Arithmetic Assignment Operator

Arithmetic Assignment Operators are used when you want to perform an operation on an operand and store it in the operand itself. It combines the arithmetic operator and assignment operator and eliminates the repetition of the operand in an expression.

The usual way of writing such an expression would be, for example:

int a = 10;
a = a + 20;

Using an Arithmetic Assignment Operator, you can shorten this expression as follows:

int a = 10;
a += 20;

C language provides arithmetic assignment operators for all arithmetic operations.

ExpressionUsing Arithmetic Assignment Operator
a = a + 2a += 2
a = a – 2a -= 2
a = a * 2a *= 2
a = a / 2a /= 2
a = a % 2a %= 2

Increment Operator

Using an increment operator we can increment a variable value by 1.

int a = 10;
a++; // a becomes 11
++a; // a becomes 12

As you see in the above code snippet it really doesn’t matter where you put the increment operator when you write them as an individual statement.

However, it will have an impact when you use them as a part of an expression.

Prefix

When you write increment operator before the variable, it is called prefix. In prefix operation, the increment will happen before its value is used in an operation.

int a = 10, b = 5, c;
c = b + ++a; // c value will be 16

Postfix

When you write increment operator after the variable, it is called postfix. In postfix operation, the increment happens after its value is used in an operation.

int a = 10, b = 5, c;
c = b + a++; // c value will be 15

Decrement Operator

Similar to increment operator, we also have decrement operator which decrements a variable by 1.

We also have pre and postfix decrement variables, which act similar to the increment variable.

int a = 10, b = 5, c;
c = b + a--; // c value will be 15
c = b + --a; // c value will be 14