In this tutorials, you will learn about the arithmetic operation in C and their usage and examples.
Arithmetic Operations in C with Example
The table below illustrates C’s arithmetic operations and simple examples to understand these essential operations better.
Operator | Description | Example |
---|---|---|
+ | To adds two operands. | X + Y |
- | To subtracts second operand from the first one. | X - Y |
* | to multiplies both operands. | X * Y |
/ | To divides the first operands by second one. | X / Y |
% | Modulus Operator Remainder. | X % Y |
++ | To increment operator by 1. | X++ |
-- | To decrement operator by 1. | X-- |
Example of Arithmetic operators in C
#include <stdio.h> int main() { int x = 9, y = 5; int z; // for output value // Addition z = x + y; printf("x+y = %d \n", z); // Subtraction z = x - y; printf("x-y = %d \n", z); // Multiplication z = x * y; printf("x*y = %d \n", z); // Division z = x / y; printf("x/y = %d \n", z); //Modulus z = x % y; printf("x Modulus y reminder= \n", z); return 0; }
Output
x+y = 14 x-y = 4 x*y = 45 x/y = 1 x Modulus y reminder= 4
Notice that x / y returns 1, not 1.80. This is because we are dealing with integer here. To change the output to float, change the Data Type of x, y, and z to float.
int main() { float x = 9.0, y = 5.0; float z; // for output value // Division z = x / y; printf("x/y = %f \n", z); return 0; }
Output
x/y = 1.800000
Note: the Modulus only works with integer Data Type.