C Arithmetic Operators
In this page:
Addition (+) and Subtraction (-)
The + and - operators perform ordinary addition and subtraction on numeric values, working the same way for int, float, or double operands, following standard mathematical rules of precedence within larger expressions.
Example: Addition (+) and Subtraction (-)
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d %d", a + b, a - b);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiplication () and Division (/)
The * operator multiplies two values, and / divides them -- but dividing two integers in C performs integer division, silently discarding any fractional remainder, which surprises many beginners expecting a decimal result.
Example: Multiplication () and Division (/)
#include <stdio.h>
int main() {
int a = 7, b = 2;
printf("%d %d", a * b, a / b);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modulo Operator (%)
% returns only the remainder left over after integer division, making it the standard way to test whether a number is even or odd (checking n % 2 == 0) or to wrap a counter back to zero after reaching a limit.
Example: Modulo Operator (%)
#include <stdio.h>
int main() {
int n = 7;
printf("%d", n % 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Increment Operator (++)
++ increases a variable's value by exactly 1. Written before the variable (++x) it applies the increase before the variable is used in the surrounding expression; written after (x++) it applies the increase afterward.
Example: Increment Operator (++)
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d", ++x, x++);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Decrement Operator (--)
-- decreases a variable's value by exactly 1, following the same prefix/postfix timing rules as ++ -- prefix decrements before the value is used in an expression, postfix decrements immediately after.
Example: Decrement Operator (--)
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d", --x, x--);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: