C Arithmetic Operators
In this page:
result = operand1 + operand2; // also - * / %
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 10, b = 3;
// Print formatted text to the screen
printf("%d %d", a + b, a - b);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 2;
// Print formatted text to the screen
printf("%d %d", a * b, a / b);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 7
int n = 7;
// Print formatted text to the screen
printf("%d", n % 2);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d %d", ++x, x++);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d %d", --x, x--);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dividing two ints like
7 / 2and expecting 3.5, when the result is 3. - Using
%with floating-point numbers, which does not compile; usefmodfrommath.h. - Dividing by zero, which is undefined behavior.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: