Arithmetic Operators in C
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:
|Name of the Operator|
|Add two operands.|
x + y
|Subtract the second operand from the first operand.|
x – y
|Multiply two operands.|
x * y
|Divide the first operand by the second operand.|
x / y
|Calculate the remainder when the first operand is divided by the second operand.|
x % y
Example of Binary Arithmetic Operator in C
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:
|Decreases the integer value of the variable by one.||–h or h–|
|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:
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:
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
Post Increment and Decrement a is 11 and res is 10 a is 10 and res is 11 Pre Increment and Decrement a is 11 and res is 11 a is 10 and res 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.
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.
Area = 50 Perimeter = 30
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.
Please Login to comment...