C Arithmetic Operators
In C programming, arithmetic operators perform mathematical operations on numeric values, such as integers and floating-point numbers. Here are the most common arithmetic operators in C.
Operator | Description | Example |
---|---|---|
+ | Addition (add two operands) | 4 + 6 result is 10 |
- | Subtraction (subtract right operand from the left operand) | 5 - 2 result is 3 |
* | Multiplication (multiply two operands) | 2 * 4 result is 8 |
/ | Division (divide left operand by right operand) | 9 / 3 result is 3 |
% | Modulus (divide left operand by right operand and get remainder) | 22 % 7 result is 1 |
Unary negation - | Unary operator. Returns the negation of its operand. | If x =3 then -x results as -3 |
++ | Pre- or post-increment | If x is 5 then x++ will first return 5 and then increment in x to make it 6. If x is 5 then ++x will first increment x and set its value to 6 and then return its value i.e., 6. |
-- | Pre- or post-decrement | If x is 5 then x-- will first return 5 and then decrement in x to make it 4. If x is 5 then --x will first decrement x and set its value to 4 and then return its value i.e., 4. |
The Addition Operator ( + ) in C
The addition operator ( + ) is used to add two operands.
You are already familiar with:
int x = 10;
You can also write,
int x = 5 + 5;
You can use variables as operands.
int x = 2;
int y = 3;
int z= x + y;
You can mix variables and numbers as operands.
int x = 2;
int y = x + 2;
You can also add float values.
float x = 2.5;
float y = 3.2;
float z= x + y; // 5.7
The Subtraction Operator ( - ) in C
The subtraction operator subtract right operand from the left operand.
Example:
int x = 3;
int y = 2;
int z = x - y; // 1
The Multiplication Operator ( * ) in C
The multiplication operator multiply two operands.
Example:
int x = 2;
int y = 3;
int z = x * y; // 6
The Division Operator ( / ) in C
The division operator divides left operand by right operand. When / is used with int or char the decimal part of the remainder value is truncated.
Example:
int x = 3;
int y = 2;
printf("%d ", x/y); // 1 instead of 1.5
If the right operand is zero, then the result is un-defined or floating point exception.
int x = 3;
int y = 0;
printf("%d ", x/y); // Floating point exception
The Modulus Operator ( % ) in C
The modulus operator divides left operand by right operand and get remainder. The modulus operator does not support float and double.
int x = 11;
int y = 3;
printf("%d ", x%y); // 2
If the right operand is zero, then the result is un-defined or floating point exception.
The Unary Negation Oprator ( - ) in C
The unary negation operator simply return negation of its operand.
Example:
int x = 2;
int y;
y = -x;
printf("%d ", y); // -2
The Pre or Post-Increment Operator ( ++ ) in C
The increment operator adds 1 to its operand. It could be pre increment or post increment.
- The pre-increment means. First increment then return the value.
- The post increment means. First return the value then increment.
Example: Pre-Increment
int x = 3;
int y;
//First value of x will be incremented by 1.
//The x will become 4, then value of x, 4 will be assigned to variable y
y = ++x;
printf("%d %d", x, y); // 4 4
Example: Post-Increment
int x = 3;
int y;
// First value of x, 3 will be assigned to y. Then x will be incremented by 1.
y = x++;
printf("%d %d", x, y); // 4 3
The Pre or Post-Decrement Operator ( -- ) in C
The decrement operator subtract 1 from its operand. It could be pre decrement or post decrement.
- The pre-decrement means. First decrement then return the value.
- The post decrement means. First return the value then decrement.
Example: Pre-Decrement
int x = 5;
int y;
//First value of x will be decremented by 1.
//The x will become 4, then value of x, 4 will be assigned to variable y.
y = --x;
printf("%d %d", x, y); // 4 4
Example: Post-Decrement
int x = 5;
int y;
//First value of x, 5 will be assigned to y. Then x will be decremented by 1.
y = x--;
printf("%d %d", x, y); // 4 5