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:
- Binary Arithmetic Operators
- 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
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
printf ( "a is %d and b is %d\n" , a, b);
res = a + b;
printf ( "a + b is %d\n" , res);
res = a - b;
printf ( "a - b is %d\n" , res);
res = a * b;
printf ( "a * b is %d\n" , res);
res = a / b;
printf ( "a / b is %d\n" , res);
res = a % b;
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
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
printf ( "Post Increment and Decrement\n" );
res = a++;
printf ( "a is %d and result is %d\n" , a,
res);
res = a--;
printf ( "a is %d and result is %d\n" , a,
res);
printf ( "\nPre Increment and Decrement\n" );
res = ++a;
printf ( "a is %d and result is %d\n" , a, res);
res = --a;
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
#include <stdio.h>
int main()
{
int var;
var = 10 * 20 + 15 / 5;
printf ( "Result = %d" , var);
return 0;
}
|
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
#include <stdio.h>
int main()
{
int length = 10;
int breadth = 5;
int area, perimeter;
area = length * breadth;
perimeter = 2 * (length + breadth);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!