Assignment, Increment, and Conditional Operators
Compound assignment, prefix and postfix ++ and --, the ternary operator, and why classic exam expressions like i++ + ++i have no defined answer.
Shorthand with Teeth
C is famous for compact operators that update variables in place. Used plainly they make code shorter and clearer; combined carelessly they produce expressions that have no defined meaning at all, which exam papers unfortunately love to present as puzzles. This lesson gives you the shorthand, and the honest line between clever and undefined.
Compound Assignment
Every arithmetic operator has an assignment form: +=, -=, *=, /=, %=. The statement total = total + amount; and total += amount; mean the same thing: compute with the current value, store the result back.
Read x op= y as "x gets x op y" and the whole family is learned at once. Course code prefers the compound form whenever a variable updates itself; it says "update" at a glance.
Increment and Decrement
Adding or subtracting exactly 1 is so common it has dedicated operators: ++ and --. Each comes in two placements with genuinely different meanings as expressions:
- Prefix
++count: increment first, and the expression's value is the new value. - Postfix
count++: the expression's value is the old value, and the increment happens as a side effect.
The first assignment takes 5 and leaves count at 6; the second bumps count to 7 and takes 7. When the value is not used, count++; alone on a line, the two forms do the same thing, and either is fine.
The Line You Must Not Cross
Because ++ both produces a value and modifies its variable, it invites combinations like these, which appear on real exam papers with answer keys attached:
/* WRONG: undefined behaviour, do not write and do not "solve" */
result = i++ + ++i;
i = i++;
These are labeled wrong for a precise reason: C only promises that a variable's updates are settled at certain points in the program (the end of a full statement is the everyday one, and &&, ||, and ?: add their own). Between those points, modifying the same variable twice, or modifying it and separately reading it, is undefined behaviour. Not "evaluated left to right", not "compiler dependent" as if there were a short list of candidate answers: undefined, the standard washes its hands entirely. Different compilers genuinely print different numbers here, and all of them are conforming.
So when a paper asks for the output of i++ + ++i, the technically correct answer is "this expression is undefined behaviour". If the exam demands a number, give the one its textbook computes, and know that you are reciting a convention, not a fact about C. In your own code the rule is simpler: one modification of any variable per statement, full stop.
The Conditional Operator
C's only three-operand operator packs a choice into an expression: condition ? valueIfTrue : valueIfFalse.
The condition is evaluated first; then exactly one of the two arms is evaluated, never both, the same short-circuit discipline && and || follow. The conditional operator shines where a value is being selected, as above. Chains of nested ?: making complex decisions are better written as the if statements of chapter 5; when the choice is bigger than one value, reach for a statement, not an expression.
Key Takeaways
x += yand family: compute-and-store shorthand, one operator per arithmetic operation.- Prefix
++xyields the new value; postfixx++yields the old value; standalone, either works. - Modifying a variable twice in one expression, or modifying and separately reading it, is undefined behaviour:
i++ + ++ihas no correct output, whatever an answer key claims. - One modification per variable per statement keeps you permanently on the defined side of the line.
cond ? a : bselects one of two values, evaluating only the chosen arm; prefer it for value selection, and statements for bigger decisions.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Assignment, Increment, and Conditional Operators - Quiz
Test your understanding of the lesson.
Practice Exercises
Pick the Larger, Measure the Gap
Read two integers and use the conditional operator twice: once to print the larger of the two, once to print their absolute difference. No if statements exist yet in this course, and none are needed.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!