Open In App

Arithmetic Operators in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Arithmetic Operators are the type of operators in C that are used to perform mathematical operations in a C program. They can be used in programs to define expressions and mathematical formulas.

What are C Arithmetic Operators?

The C arithmetic operators are the symbols that are used to perform mathematical operations on operands. 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.

x + y

Subtraction

Subtract the second operand from the first operand.

x y

*

Multiplication

Multiply two operands.

x * y

/

Division

Divide the first operand by the second operand.

x / y

%

Modulus

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

x % y

Example of Binary Arithmetic Operator in C

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




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




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




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

Conclusion

In this article, we discussed the arithmetic operators in C and how they are useful in performing both basic and complex calculations in the C program. These are one of the core concepts that build the foundation of the C language.

FAQs on C Arithmetic Operators

1. List all the arithmetic operators in C.

Below is the list of all the arithmetic operators in C language:

  • ( + ) Addition Operator
  • ( – ) Subtraction Operator
  • ( * ) Multiplication Operator
  • ( / ) Division Operator
  • ( % ) Modulo Operator
  • ( ++ ) Increment Operator
  • ( — ) Decrement Operator
  • ( + ) Unary Plus Operator
  • ( – ) Unary Minus Operator

2. What is the difference between the unary minus and subtraction operators?

Although the unary minus and subtraction operator looks the same, their function is different from each other in the following ways:

  • As the name suggests, the unary operator works on a single operator while the subtraction operator is a binary operator that works on two operands.
  • The unary minus returns the negative of the value of its operand while the subtraction operator returns the difference between its two operands.

Related Articles:

 Quiz on Operators in C



Last Updated : 16 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads