Open In App

Arithmetic Operators in C

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Arithmetic operators are the type of operators used to perform basic math operations like addition, subtraction, and multiplication. Let’s take a look at an example:

C
#include <stdio.h>
int main() {

    // Calculate the area of the triangle
    int sum = 10 + 20;

    printf("%d", sum);
    return 0;
}

Output
30

Explanation: In this code, the + operator is used for arithmetic addition, adding the integers 10 and 20 resulting in value 30 which is stored in the variable sum.

C provides 9 arithmetic operators to work with numbers and perform different mathematical operations. These can be classified into two types based on the number of operands they work on:

  1. Binary Arithmetic Operators
  2. Unary Arithmetic Operators

1. Binary Arithmetic Operators

The binary arithmetic operators work on two operands. C provides 5 such operators for performing arithmetic functions which are as follows:

Name

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

C
#include <stdio.h>
int main(){
    int a = 10, b = 4, res;

    // Addition
    res = a + b; 
    printf("a + b is %d\n", res);
  
    // Subtraction
    res = a - b; 
    printf("a - b is %d\n", res);

    // Multiplication
    res = a * b; 
    printf("a * b is %d\n", res);

    // Division
    res = a / b; 
    printf("a / b is %d\n", res);

    // Modulus
    res = a % b; 
    printf("a %% b is %d\n", res);
    return 0;
}

Output
a + b is 14
a - b is 6
a * b is 40
a / b is 2
a % b is 2

Note: All arithmetic operators can be used with float and int types except the modulo operator that can only be used with integers.

2. Unary Arithmetic Operators

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

NameOperatorDescriptionSyntax
Decrement

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

++

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

+

Returns the value of its operand.+a
Unary Minus

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

Example

C
#include <stdio.h>

int main(){
    int a = 10, res;
  
    // post-incrementing a
    // res is assigned 10, a is not updated yet
    res = a++;
  	// a becomes 11 now
  
    printf("a is %d, res is %d\n", a,res); 

    // post-decrement example:
    // res is assigned 11, a is not updated yet
    res = a--;
  	// a becomes 10 now
    printf("a is %d, res is %d\n", a, res); 
  
    // 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, res 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, res is %d\n", a, res);
  
  	printf("+a is %d\n", +a);
  	printf("-a is %d", -a);

    return 0;
}

Output
a is 11, res is 10
a is 10, res is 11
a is 11, res is 11
a is 10, res is 10
+a is 10
-a is -10

Explanation: In this C program, the post-increment and post-decrement operators are demonstrated, where the value of a is updated after it is assigned to res. In contrast, the pre-increment and pre-decrement operators update a first before assigning it to res. The program prints the value of a and res after each operation to show how the operators affect the values of the variables.

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
#include <stdio.h>
int main(){
    int var;

    // expression with multiple operators
    var = 10 * 20 + 15 / 5;

    printf("%d", var);
    return 0;
}

Output
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 unambiguous expressions.



Next Article
Article Tags :

Similar Reads