← The C Language
Lesson 11 of 77 · 10 XP
Operators
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
Arithmetic: + - * / %. Remember: with integers, / drops the fractional part, and % gives the remainder (integers only).
Compound assignment: x += 5, x -= 1, x *= 2, x /= 3, x %= 10.
Increment and decrement — something Python doesn't have:
int i = 5;
i++; // i = 6
i--; // i = 5
int a = i++; // a = 5, i = 6 — postfix: the value first, then +1
int b = ++i; // b = 7, i = 7 — prefix: +1 first, then the value
On a line of its own, i++ and ++i are the same. Don't use them twice in one expression: i = i++ + ++i; is undefined behaviour, and the result depends on the compiler.
Comparisons: == != < > <= >=. Logical: && (and), || (or), ! (not).
The ternary operator is a short if:
int max = (a > b) ? a : b;
Task. Read three whole numbers and print the largest. Use the ternary operator.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.