Expressions and operators: meaning before precedence
Reason about operands, types, comparisons, short-circuiting, side effects, and the difference between grouping and evaluation order.
In this chapter11 sections
Operators tell C which operation to perform on values or objects. Some calculate, some compare, and some change stored information. We will separate what an operator does from how an expression groups and when its parts are evaluated.
Need the ideas side by side? Open the concept guide.
1. Start with the operands and their types
An operand is what an operator works on. In 7 + 2, seven and two are operands and + is the operator. Operand types affect the operation. Read both the symbol and the types before deciding what result to expect.
Conversions may happen before the operation and again when a result is stored. Assigning an integer division result to a double does not change the division that already took place. The destination type does not reach backward through the expression and choose different operand rules.
int whole = 7 / 2;
double converted = 7 / 2;
double fractional = 7.0 / 2;The first division has integer operands and produces three. The second performs that same integer division, then converts the result for storage. The third uses floating-point division. For integer division, the fractional part is discarded toward zero, so -7 / 2 is −3. The remainder operator % works on integer operands, not as a general percentage operator.
2. Comparison asks a question
== tests equality and != inequality; <, <=, >, and >= express ordering comparisons. These operators produce an int result of zero or one. An assignment with = is a different operation and itself has a value, which is why accidentally using it in a condition can form valid but unintended C.
C does not interpret 0 < age < 18 as a mathematical range test. It first compares zero with age, producing zero or one. It then compares that result with 18. Write the intended two tests as 0 < age && age < 18.
int age = 12;
int within_range = age > 0 && age < 18;3. Logical operators test truth and can short-circuit
In a scalar condition, zero means false and nonzero means true. Negative integers count as true too. && evaluates its right side only when the left side is true. || evaluates its right side only when the left side is false. These operators produce zero or one. ! reverses a truth test and also produces zero or one.
int total = 12;
int divisor = 0;
int large_quotient = divisor != 0 && total / divisor > 2;The false left operand prevents this division from being evaluated, so the example does not divide by zero. This is a sequencing guarantee of these logical operators, not a general property of every operator written left to right. Bitwise & and | have different meanings and do not provide this short-circuit protection.
int logical_result = 2 && 4;
int bitwise_result = 2 & 4;Both numbers are nonzero, so the logical result is one. Their relevant set bits do not overlap, so the bitwise result is zero. A defined bitwise result is not the same question as whether both values count as true. Also, ^ is bitwise exclusive OR; C has no exponentiation operator.
4. Increment has both a value and an effect
int count = 4;
int before = count++;
int after = ++count;Postfix increment, count++, supplies the old value and also increments the object. Prefix increment, ++count, supplies the incremented value. In these separate statements, before becomes four, after becomes six, and count ends at six. Keeping the steps separate makes the change visible.
Compound assignment such as count += 2 combines an operation and a store. For a simple integer object within range it expresses the intended update clearly. In general, the left operand is evaluated once, and the conversion rules matter, so do not treat textual replacement with count = count + 2 as a proof for every possible complex operand.
5. Precedence describes grouping
int first = 2 + 3 * 4;
int second = (2 + 3) * 4;
int third = 20 - 5 - 3;Multiplication groups more tightly than addition, so 2 + 3 * 4 gives 14. Parentheses change the grouping in (2 + 3) * 4. Subtraction groups left to right in 20 - 5 - 3. These grouping rules do not generally decide which operand runs first.
A grouped expression can still leave evaluation order open. For combine(left(), right()), C does not promise to call left first. Independent calls can allow either order. Conflicting accesses without the required sequencing can instead cause undefined behavior; they are not just two alternative outputs.
6. Choose clarity over a dense expression
The conditional operator condition ? first : second evaluates the condition and only the selected one of the other operands; its result also follows type rules. Operators such as sizeof, address-of, and dereference have their own roles and constraints, covered in the connected chapters. Do not learn the precedence table as a substitute for those meanings.
Do extra parentheses around a function argument guarantee it will be evaluated before the other argument?
Read the explanation
No. Grouping and sequencing are different. Use the language’s sequencing rules or separate full expressions when an order is required.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: Sections 6.3.1.8 and 6.5–6.5.17 provide the core rules used in this C17-baseline explanation. This is a public draft, not the final ISO publication.
- WG14 N3096 — public C23 draft: Expression sequencing and version-specific language rules; the cited undefined example is not repaired by changing from C17 to C23.
Follow the type before the result
Explore the visual model
VISUAL MODEL
Separate the operands, the operation, and conversion to the destination. These are reasoning stages, not separate machine instructions.
Each case is a fragment inside a function. The unsigned-char case specifically assumes CHAR_BIT == 8; <limits.h> supplies that macro.
int numerator = 5, denominator = 2;double ratio = numerator / denominator;
numerator5Operand valuedenominator2Operand valuenumerator / denominator—Not produced yetratio—Not produced yetBoth operands are int. ratio will have type double, but that does not change the division’s operand types.
A cast does not check a range. The original objects are not changed by operand conversion. C does not require 8-bit bytes; only the labeled unsigned-char case assumes them. Signed overflow remains undefined, including in C23.
Read the complete trace
- Both operands are int. ratio will have type double, but that does not change the division’s operand types.
- 5 / 2 is integer division, so the quotient is the int value 2. This card represents the expression result, not a changed declaration of ratio.
- The int quotient 2 converts to double and initializes ratio with 2.0. The fraction was already discarded.
The expression’s type comes before its destination
Compare, follow, and explain this idea
C can convert values before an operation and again when storing its result. The destination type does not travel backwards and change an earlier operation.
When this helps Use this order when reading integer division, mixed signed/unsigned comparisons, casts, and assignments to smaller types.
Read the ideas side by side
- Integer division
- With a nonzero divisor and a representable quotient, int division produces an int result, discarding the fractional part toward zero.
- Floating division
- A floating operand can cause the other arithmetic operand to be converted before division.
A double destination alone does not make 5 / 2 a floating operation.
- Implicit conversion
- The language rules request a conversion at an operation, call, return, or assignment.
- Explicit cast
- The source explicitly requests a conversion to a named type.
An explicit conversion still follows rules and can lose information.
- Integer promotion
- Some integer types are converted to int or unsigned int before many operations.
- Usual arithmetic conversions
- For operators that use them, rules find a common arithmetic type after the applicable promotions.
“The largest-looking variable wins” is not the rule.
Follow the reasoning
Use only the two displayed int operands. This walkthrough assumes ordinary binary double arithmetic, where 2.5 is exactly representable.
C17 · illustrative fragments in the stated context
int numerator = 5, denominator = 2;double first = numerator / denominator;double second = (double)numerator / denominator;
- Read the operand declarations
numerator and denominator are int objects holding 5 and 2.
- Evaluate the first division
Both operands are int, so numerator / denominator produces the int value 2.
- Initialize first
The already-computed 2 is converted to double. first receives 2.0; the lost fraction cannot be recovered.
- Convert before the second division
(double)numerator produces 5.0. For this division, denominator is converted to double as well.
- Initialize second
The floating division yields 2.5 and initializes second. The cast’s position changes which operation is performed.
Common misconceptions
These statements are misleading. Open each one to see why.
MisconceptionThe left-hand type decides how the whole right side is calculated.
Evaluate the right-hand expression using its own operand types first. Then apply the conversion needed by the destination.
MisconceptionA cast proves that a value fits.
A cast requests conversion without checking a range for you. Check the relevant limits before converting when value preservation matters.
MisconceptionUnsigned always makes arithmetic safer.
Unsigned arithmetic has wraparound rules, but a wrapped count can still be logically wrong. Mixed signed/unsigned operations can also change a comparison’s meaning.
Go a little deeperOptional technical detail
Signed and unsigned of the same rank
For int and unsigned int in a usual arithmetic conversion, the common type is unsigned int. In -1 < 1U, -1 becomes UINT_MAX and the comparison is false. This does not modify the original int object.
Conversion is not signed overflow
Converting an integer to an unsigned type uses modular reduction. An out-of-range conversion to a signed integer type has an implementation-defined result or signal. Signed arithmetic overflow is a different rule: undefined behavior.
C17 The examples use standard int, unsigned int, and double rules. Do not assume an 8-bit byte or a 32-bit int when applying them elsewhere.
C23 C23 requires two’s-complement signed integer representation, but signed overflow is still undefined. Out-of-range integer-to-signed conversion remains implementation-defined or can raise an implementation-defined signal. _BitInt has additional promotion rules beyond this guide.
Public committee drafts; clause references help you verify the explanation. N3096 is a C23 working draft.
Say it in your own words
Why is casting the completed integer quotient to double too late to produce 2.5?
Compare with an explanation
The integer division already produced 2. A later conversion preserves that value; it does not repeat division with floating operands.
Before you move on
- Operand types and conversions help determine an operation’s meaning.
- Precedence and associativity describe grouping, rather than generally fixing evaluation order.
- Short-circuit logical operators provide specific evaluation guarantees; bitwise operators have different jobs.
Keep these ideas separate
- Grouping / Sequencing
- Parentheses can change grouping without establishing which independent operand is evaluated first.
Why can a guard using && prevent a division, while adding parentheses alone may not establish an evaluation order?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, precedence is a timeline.
Precedence groups expressions; evaluation order is a separate set of rules.
- No. Grouping and evaluation order are separate rules.
✓ Correct reasoning. Parentheses and precedence help determine expression structure; sequencing rules determine required ordering.
- Adding parentheses always fixes unsequenced side effects.
Grouping alone does not introduce sequencing where the language does not specify it.
C17
Quick reference from the earlier edition
This compact reference keeps existing links and saved passages available. Read the expanded lesson above for the full context.
C's operators are summarized below from highest to lowest precedence; operators sharing a row share precedence, and associativity determines how operators at the same precedence group together.
| Category | Operators | Associativity |
|---|---|---|
| Postfix | () [] -> . ++ -- |
Left to right |
| Unary | ++ -- + - ! ~ * & sizeof (type) |
Right to left |
| Multiplicative | * / % |
Left to right |
| Additive | + - |
Left to right |
| Shift | << >> |
Left to right |
| Relational | < <= > >= |
Left to right |
| Equality | == != |
Left to right |
| Bitwise AND | & |
Left to right |
| Bitwise XOR | ^ |
Left to right |
| Bitwise OR | | |
Left to right |
| Logical AND | && |
Left to right |
| Logical OR | || |
Left to right |
| Conditional | ?: |
Right to left |
| Assignment | = += -= *= /= %= <<= >>= &= ^= |= |
Right to left |
| Comma | , |
Left to right |
A few operators deserve special mention:
- The ternary operator
?:is C's only three-operand operator, offering an expression form of if-else:int max = (a > b) ? a : b;. - The comma operator evaluates its left operand, discards the result, then evaluates and returns the right operand — most often seen in
forloop headers:for (i = 0, j = 10; i < j; i++, j--). - Short-circuit evaluation:
&&and||skip evaluating their right operand once the result is already determined by the left one, a behavior programs routinely depend on, as inif (p != NULL && p->value > 0).
Sequence Points and Evaluation Order
Precedence and associativity describe grouping, not the order of operand evaluation. In C17 and C23, i = i++ + 1; and printf("%d %d", i++, i++); both have undefined behavior because conflicting accesses/modifications of i are unsequenced. They are not merely two possible, unspecified outputs. Multiple modifications in one expression are not automatically invalid: (i++, i++) uses the comma operator to sequence them. For unspecified argument order without such a conflict, consider f(g(), h()) where the calls have no conflicting effects.
Ready for the next idea?
Mark this chapter as read whenever you feel ready.