Ever wonder why 5 + 2 * 3 becomes 11 when printed with printf in C? Operator precedence makes multiplication go first, then addition. This quick guide walks you through the rules, shows the step-by-step evaluation, and helps you predict outcomes with confidence.

Multiple Choice

What is the result of the expression `printf("%d", 5 + 2 * 3);` in C?

In the expression `printf("%d", 5 + 2 * 3);`, the result is determined by the rules of operator precedence and associativity in C. The multiplication operator (`*`) has a higher precedence than the addition operator (`+`), which means that the multiplication will be performed first. Breaking down the expression: 1. The multiplication `2 * 3` is evaluated first, which equals `6`. 2. Then, the result of the multiplication is added to `5`: `5 + 6`, resulting in `11`. Thus, the value printed by the `printf` function will be `11`. This demonstrates the importance of understanding operator precedence in expressions to correctly evaluate and predict the output of C code.

Why precedence matters in C (even for a tiny line of code)

If you’re just starting out with C, you’ve probably learned a handful of rules that feel like a secret handshake. One of the most important is operator precedence—the idea that some operations get done before others simply because of the rules of the language. It’s the kind of thing that can trip you up if you’re not paying attention, yet it’s also incredibly empowering once you get the hang of it. Today we’ll look at a tiny, classic example: printing the result of 5 + 2 * 3.

Let’s set the scene with a simple line of code:

printf("%d", 5 + 2 * 3);

On the surface, it looks like a straightforward calculation, right? Five plus two times three. But in C, not everything is done in the order you might naturally think. The language has a well-defined set of rules that tell the compiler how to evaluate expressions like this. The key player here is operator precedence.

The rulebook: precedence and associativity

Think of operator precedence as the order you use when you stack building blocks. Some blocks are bigger and go on first, others sit on top. In C, the multiplication operator (*) sits higher in the hierarchy than the addition operator (+). That means the compiler will do the multiplication before the addition.

So, in our expression, the multiplication 2 * 3 is evaluated first. That gives 6. Then we add the 5 to that result, yielding 11. The final value passed to printf is 11, so you’ll see 11 printed on the screen.

A deeper look: why this makes sense

You might wonder, “Why this particular order?” The designers of C wanted arithmetic to resemble standard mathematical conventions where multiplication happens before addition. It’s a natural expectation: if you have 5 + 2 × 3, you’re supposed to compute the product first, then add the result to 5. Keeping this rule consistent across expressions helps keep code readable and predictable. When you write something like 5 + 2 * 3, you’re relying on a shared understanding that halting the multiplication for a moment would be more confusing than helpful.

But math class isn’t the only place where precedence matters. In programming, you’ll frequently mix arithmetic with comparisons, logical operations, bitwise tricks, and more. Each operator has its own place in the ladder, and your job as a programmer is to respect that ladder unless you explicitly bend it with parentheses.

What if we want a different outcome?

If you want a different result, you can force the order by using parentheses. For example:

  • (5 + 2) * 3 evaluates to 21: you add first, then multiply.

  • 5 + (2 * 3) is the same as the original 11: the parentheses here clarify that the multiplication should happen first, just like the default rule.

Parentheses are your friends. They’re the explicit instruction that says, “Ignore the usual order for this part of the expression.” When you’re debugging or trying to make the intent of your code crystal clear, adding parentheses is a small effort with a big payoff.

A quick tour of related operators

Operator precedence isn’t about numbers in a vacuum. It stretches across different kinds of operators you’ll encounter as you program:

  • Multiplication, division, and modulus sit near the top among arithmetic operators. They’re performed before addition and subtraction.

  • Addition and subtraction come next.

  • Relational operators (like <, >, ==) compare values after the arithmetic part is settled.

  • Logical operators (&&, ||) come after the relational ones, because they depend on boolean logic that’s usually derived from earlier computations.

  • Bitwise operators, assignments, and other tools have their own spots in the hierarchy, which you’ll memorize piece by piece as you code more.

A practical mental model: read, then compute

A handy approach when you’re thinking through an expression is to read it in chunks:

  • First, identify any high-precedence operations (like multiplication, division, or modulus).

  • Compute those parts, then move to the next tier (additions and subtractions).

  • Finally, apply comparisons or logical operations if they show up.

This “peel the onion” method helps you anticipate what will happen without having to run the code every time. It also slows down those quick, reflexive guesses that lead you astray.

Why this matters for a newcomer’s toolkit

Understanding precedence isn’t just about predicting a one-off printout. It builds a foundation for safer, more readable code. Here’s how it pays off in the real world:

  • Debugging becomes easier. If you know that 5 + 2 * 3 is 11, you can spot a mismatch quickly when something prints as 21 because you used parentheses differently.

  • Your intent shines through. When you add parentheses to express your exact calculation, readers (including future you) don’t have to reconstruct your hidden priorities.

  • It reduces surprises when mixing types. If you start combining integers with floating-point numbers, the rules of promotion and type conversion layer on top of precedence. Knowing the base case helps you predict outcomes more reliably.

A few notes about types and promotions

In our example, everything is an int (the default for numeric literals like 5, 2, and 3). The arithmetic is performed with integer math, so no fractions appear. If you had a floating-point number involved, the rules would still apply, but the result could be a float, and you’d need to consider the formatting specifier in printf.

For instance:

printf("%f", 5 + 2 * 3.0);

Here, one operand is a double. The expression is still computed with multiplication taking precedence, giving 5 + 6.0, which is 11.0 as a double. The print specifier %f tells printf to format that floating-point result.

Common pitfalls to look out for

Even seasoned programmers trip over small slips here. A few frequent culprits:

  • Overlooking hidden precedence. If you’re skimming code, you might miss that multiplication is happening before addition, leading to a result you didn’t expect.

  • Assuming left-to-right evaluation for all operators. In C, not all operators share the same precedence, and even among those with the same precedence, associativity rules can change how the expression is evaluated.

  • Mixing ints and floats without intent. Implicit promotions can sneak in, producing results you didn’t mean if you’re not mindful of the types involved.

  • Relying on “feel” rather than explicit structure. When in doubt, drop in parentheses to make the calculation crystal clear.

A tiny, practical mindset shift

Think of expressions like recipes. The recipe lists ingredients (operands) and cooking steps (operators). The order matters because some steps transform ingredients into more important forms than others. Multiplication is a way to mash several pieces of the same thing together quickly; addition is a way to combine the results with something else. The kitchen rules say: do the high-impact steps first, then the lighter ones. That’s operator precedence in action.

A quick practice run you can try (mentally or in code)

  • Evaluate: printf("%d\n", 10 - 4 * 2); What happens? Expect 2, because 4 * 2 = 8, then 10 - 8 = 2.

  • Now flip the order with parentheses: printf("%d\n", (10 - 4) * 2); Expect 12, because 10 - 4 = 6, then 6 * 2 = 12.

  • Feeling curious about mixed types? Try: printf("%d\n", 5 + 2 * 3.0); This will print 11, but the value is a double (11.0) promoted to the printing format; you’d typically use %f to show the decimal part clearly.

Bringing it home with a broader perspective

Operator precedence isn’t just an abstract rule carved into stone. It’s a living, breathing part of how you communicate ideas through code. When you write a line like printf("%d", 5 + 2 * 3); you’re not just asking the computer to do math; you’re inviting the reader to follow your thinking. The structure of your expression is a tiny narrative—the multiplication acts as the heavy lift, the addition as the final touch before the result appears.

And as you build more complex programs, you’ll add more layers to that narrative. You’ll combine arithmetic with conditionals, loops, and function calls. You’ll encounter compound expressions where precedence plays a starring role in how the logic unfolds. The more fluent you become with these invisible rules, the more you’ll enjoy the clarity and control they offer.

A friendly reminder: tools and everyday debugging

When you’re coding, the simplest tools can save you from midnight mysteries. A quick run in a compiler or an integrated development environment that highlights precedence can be a real ally. Don’t hesitate to insert temporary print statements or comment blocks to remind yourself of the intended grouping. And if you’re ever unsure, jot down the steps on a scratch pad: “First compute this, then that.” It’s surprising how often a few lines of reasoning can prevent a misstep.

Closing thought: small rules, big confidence

The elegance of C lies in its disciplined simplicity. Operator precedence is one of those tiny, reliable guidelines that quietly anchors your understanding, even when you’re juggling more complex ideas. The next time you see a line like printf("%d", 5 + 2 * 3); you’ll know exactly what’s happening underneath the surface. Not because you memorized a distant rule, but because you’ve built a mental model you can trust. And that trust is the kind of thing that makes programming feel less like guesswork and more like thoughtful craftsmanship.