C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

Arithmetic Operators In C

 

When programming in the C language, the arithmetic operators help in performing mathematical functions. Arithmetic Operators in C allows a user to construct various formulas and mathematical equations. 

 

There are a total of 9 arithmetic operators in C to provide the basic arithmetic operations such as addition, subtraction, multiplication, etc.

 

Types of Arithmetic Operators in C

The C Arithmetic Operators are of two types based on the number of operands they work. These are as follows:

  1. Binary Arithmetic Operators
  2. Unary Arithmetic Operators

1. Binary Arithmetic Operators in C

The C binary arithmetic operators operate or work on two operands. C provides 5 Binary Arithmetic Operators for performing arithmetic functions which are as follows:

 

Operator

Name of the Operator

Arithmetic Operation

Syntax

+

Addition

Add two operands.

y

Subtraction

Subtract the second operand from the first operand.

 y

*

Multiplication

Multiply two operands.

* y

/

Division

Divide the first operand by the second operand.

y

%

Modulus

Calculate the remainder when the first operand is divided by the second operand.

% y

 

Example of Binary Arithmetic Operator in C

 

// C program to demonstrate syntax of binary arithmetic
// operators
#include <stdio.h>
 
int main()
{
    int a = 10, b = 4, res;
 
    // printing a and b
    printf("a is %d and b is %d\n", a, b);
 
    res = a + b; // addition
    printf("a + b is %d\n", res);
 
    res = a - b; // subtraction
    printf("a - b is %d\n", res);
 
    res = a * b; // multiplication
    printf("a * b is %d\n", res);
 
    res = a / b; // division
    printf("a / b is %d\n", res);
 
    res = a % b; // modulus
    printf("a %% b is %d\n", res);
 
    return 0;
}
 
 
Output
a is 10 and b is 4
a + b is 14
a - b is 6
a * b is 40
a / b is 2
a % b is 2

2. Unary Arithmetic Operators in C

The unary arithmetic operators operate or work with a single operand. In C, we have two unary arithmetic operators which are as follows:

Operator Symbol Operation Implementation
Decrement Operator

Decreases the integer value of the variable by one. –h or h–
Increment Operator

++

Increases the integer value of the variable by one. ++h or h++
Unary Plus Operator

+

Returns the value of its operand. +h
Unary Minus Operator

Returns the negative of the value of its operand. -h

Increment Operator in C

The ‘++’ operator is used to increment the value of an integer. It can be used in two ways:

1. Pre-Increment

When placed before the variable name (also called the pre-increment operator), its value is incremented instantly. Consider the example:

a = ++x;

This example can be expanded to

a = (x = x + 1);

2. Post Increment

When it is placed after the variable name (also called the post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example:

a = x++;

It can be expanded to

a = x;
x = x + 1;

Decrement Operator in C

The ‘–‘ operator is used to decrement the value of an integer. Just like the increment operator, the decrement operator can also be used in two ways:

1. Pre-Decrement

When placed before the variable name (also called the pre-decrement operator), its value is decremented instantly. For example, – – x.

2. Post Decrement

When it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x – –.

Example of Unary Operators in C

 

// C program to demonstrate working
// of Unary arithmetic
// operators
#include <stdio.h>
 
int main()
{
    int a = 10, b = 4, res;
 
    printf("Post Increment and Decrement\n");
    // post-increment example:
    // res is assigned 10 only, a is not updated yet
    res = a++;
    printf("a is %d and result is %d\n", a,
           res); // a becomes 11 now
 
    // post-decrement example:
    // res is assigned 11 only, a is not updated yet
    res = a--;
    printf("a is %d and result is %d\n", a,
           res); // a becomes 10 now
 
    printf("\nPre Increment and Decrement\n");
    // pre-increment example:
    // res is assigned 11 now since
    // a is updated here itself
    res = ++a;
 
    // a and res have same values = 11
    printf("a is %d and result is %d\n", a, res);
 
    // pre-decrement example:
    // res is assigned 10 only since a is updated here
    // itself
    res = --a;
 
    // a and res have same values = 10
    printf("a is %d and result is %d\n", a, res);
 
    return 0;
}
Output
Post Increment and Decrement
a is 11 and result is 10
a is 10 and result is 11

Pre Increment and Decrement
a is 11 and result is 11
a is 10 and result is 10

Multiple Operators in a Single Expression

Till now, we have only seen expressions in which we have used a single operator in a single expression. What happens when we use multiple operators in a single expression? Let’s try to understand this with the help of the below example.

Example

 

// C program to demonstrate the use
#include <stdio.h>
 
int main()
{
    int var;
 
    // expression with multiple operators
    var = 10 * 20 + 15 / 5;
 
    printf("Result = %d", var);
    return 0;
}
Output
Result = 203

Explanation: The order of evaluation of the given expression is : ( ( 10 * 20 ) + (15 / 5 ) ).

This is due to the Operator Precedence and Associativity concept in C language where the operators with higher precedence will be evaluated first. The operator precedence system helps to provide unambiguously expressions.

Examples of C Arithmetic Operators

Example 1: C Program to find the area of a rectangle and triangle.

We will use the arithmetic operators for calculating the area and perimeter of the rectangle using the standard formula of each.

 

 

// C Program to calculate the area and perimeter of the
// rectangle
#include <stdio.h>
 
int main()
{
    // declaring dimensions of the rectangle
    int length = 10;
    int breadth = 5;
 
    // declaring variables to store the results
    int area, perimeter;
 
    // calculating area
    area = length * breadth;
 
    // calculating perimeter
    perimeter = 2 * (length + breadth);
 
    // printing results
    printf("Area = %d\nPerimeter = %d", area, perimeter);
 
    return 0;
}
Output
Area = 50
Perimeter = 30