Lesson 3 of 31
Arithmetic
Arithmetic Operators
C supports the standard arithmetic operators:
| Operator | Operation |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
Spock calculating warp field equations: precise arithmetic is critical when lives are at stake.
Integer Arithmetic
When both operands are integers, the result is an integer. Division truncates toward zero:
int a = 17;
int b = 5;
printf("%d\n", a / b); // 3 (not 3.4)
printf("%d\n", a % b); // 2 (remainder)
Operator Precedence
C follows standard mathematical precedence: *, /, % are evaluated before +, -. Use parentheses to override:
int result = 2 + 3 * 4; // 14, not 20
int other = (2 + 3) * 4; // 20
Type Casting
You can convert between types using a cast:
int a = 7;
int b = 2;
printf("%d\n", a / b); // 3 (integer division)
Your Task
Given a = 17 and b = 5, print the results of addition, subtraction, multiplication, integer division, and modulus, each on a separate line.
TCC compiler loading...
Loading...
Click "Run" to execute your code.