Arithmetic Operators in C
Arithmetic Operators in C
Arithmetic Operators in C
Arithmetic operators in C are certain special symbols, predefined to perform
arithmetic operations. We are familiar with the basic arithmetic operations −
addition, subtraction, multiplication and division. C is a computational language, so
these operators are essential in performing a computerised process.
Operator Description
The ++ and -- operators are also listed in the above table. We shall learn about
increment and decrement operators in a separate chapter.
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 1/6
6/16/24, 11:49 AM Arithmetic Operators in C
int op2 = 3;
return 0;
}
Output
When you run this code, it will produce the following output −
Operand1: 10 Operand2: 3
Type Casting in C
The first three results are as expected, but the result of division is not. You expect
10/3 to be a fractional number (3.333333). Is it because we used the %d format
specifier to print the outcome of the division? If we change the last line of the code
as follows −
Now the outcome of the division operation will be "0.000000", which is even more
surprising. The reason why C behaves like this is because the division of an integer
with another integer always returns an integer.
To obtain floating-point division, at least one operand must be a float, or you need to
use the typecast operator to change one of the integer operands to float.
Now, change the last printf statement of the given program as follows −
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 2/6
6/16/24, 11:49 AM Arithmetic Operators in C
When you run run the code again after making this change, it will show the correct
division −
Note: If you use %d format specifier for a floating-point expression, it will always
result in "0".
Example
The result of arithmetic operations with at least one float (or double) operand is
always float. Take a look at the following example −
#include <stdio.h>
int main(){
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 3/6
6/16/24, 11:49 AM Arithmetic Operators in C
In C, char data type is a subset of int type. Hence, we can perform arithmetic
operations with char operands.
Example
The following example shows how you can perform arithmetic operations with two
operands out of which one is a "char" type −
#include <stdio.h>
int main(){
return 0;
}
Output
operand1: F operand2: 3
Since a char data type is a subset of int, the %c format specifier returns the ASCII
character associated with an integer returned by the %d specifier.
If any arithmetic operation between two char operands results in an integer beyond
the range of char, the %c specifier displays blank.
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 4/6
6/16/24, 11:49 AM Arithmetic Operators in C
Modulo Operator in C
The modulo operator (%) returns the remainder of a division operation.
Example
#include <stdio.h>
int main(){
return 0;
}
Output
Operand1: 10 Operand2: 3
Modulo of op1 and op2: 1
The modulo operator needs both the operands of int type. If not, the compiler gives
a type mismatch error. For example, change the data type of "op1" to float in the
above code and run the program again −
Now, you will get a type mismatch error with the following message −
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 5/6
6/16/24, 11:49 AM Arithmetic Operators in C
Negation Operator in C
The increment and decrement operators represented by the symbols ++ and -- are
unary operators. They have been covered in a separate chapter. The "−" symbol,
representing subtraction operator, also acts a unary negation operator.
Example
The following example highlights how you can use the negation operator in C −
#include <stdio.h>
int main(){
int op1 = 5;
int op2 = -op1;
return 0;
}
Output
When you run this code, it will produce the following output −
Operand1: 5 Operand2: -5
In the above example, the "–" symbol returns the negative value of op1 and assigns
the same to op2.
https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm 6/6