What is C Expression Evaluation Walkthrough- Free C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Chapter 4 · Operators and Expressions

C Expression Evaluation Walkthrough

Predict the exact value of any C expression, one operator at a time

Ch 4
Operators and Expressions
6
Worked programs
Beginner
Level

Welcome to the next lecture of our free C programming course from EmbeddedPathashala. You already know the operators, their precedence and their associativity. Now we put all of that to work: given a long expression full of +, *, %, >= and &&, how do you work out the exact value the compiler will produce, one operator at a time?

This is the skill that separates people who guess at C output from people who predict it. It is also the everyday skill of embedded C programming: sensor scaling, flag tests and timer arithmetic are all just expressions, and a wrong grouping means a wrong reading on real hardware. This free C tutorial gives you a repeatable method, three worked examples and an ADC conversion bug that bites almost every beginner.

What You Will Learn in This Free C Programming Course Lecture

  • A five-step method to evaluate any C expression by hand
  • How to number the evaluation steps and which steps are independent of each other
  • How arithmetic, relational and logical operators combine in one expression
  • Why raw / 4095 * 3300 returns 0 and how to fix it
  • How to confirm your hand evaluation by compiling and running real code
  • Common mistakes and the compiler warnings that catch them

Prerequisites for This Free C Programming Course Lecture

  • The operator precedence and associativity lectures from this chapter
  • Integer division and the modulus operator behaviour
  • A working C compiler such as GCC or Clang (every example here was compiled with both)

Why Evaluation Skill Matters in Embedded C Programming

On a desktop a wrong expression gives a wrong number on screen. On a microcontroller it can give a motor that spins the wrong way, a battery gauge stuck at zero, or a watchdog that never gets fed. Firmware is dense with compact expressions such as (status & READY) != 0 or ticks * 1000 / clock_hz. If you cannot evaluate them in your head the way the compiler does, you will spend hours in a debugger finding a bug that was visible on paper.

The compiler follows exactly two rulebooks: precedence decides which operator grabs its operands first, and associativity breaks ties between operators of the same level. Everything else is bookkeeping. The method below turns that bookkeeping into a routine.

The Five-Step Method to Evaluate Any C Expression

  1. Substitute the current values of the variables into the expression.
  2. Resolve parentheses first, starting from the innermost pair.
  3. Pick the operator with the highest precedence that is still unevaluated. On a tie, use associativity: left to right for binary arithmetic, relational and logical operators, right to left for unary operators and assignment.
  4. Replace that operator and its operands with the value it produces, and number the step.
  5. Repeat until a single value is left. Check the type: integer division and promotions change the answer.

Here is the part of the precedence table you need for this lecture, from tightest binding to loosest:

LevelOperatorsAssociativity
1() [] postfix ++ --left to right
2unary ! ~ + - prefix ++ -- (type) sizeofright to left
3* / %left to right
4+ -left to right
5<< >>left to right
6< <= > >=left to right
7== !=left to right
8 to 10& then ^ then |left to right
11&&left to right
12||left to right
13?:right to left
14= += -= *= and friendsright to left
15, (comma)left to right

Worked Example 1: Pure Arithmetic Step by Step

Take a = 7, b = 3, c = 2 and evaluate a + b * c - a / b % c. Level 3 operators (*, /, %) run before level 4 (+, -). Inside a / b % c the two operators share level 3, so associativity makes it (a / b) % c.

Evaluation ladder: a + b * c – a / b % c
start   → 7 + 3 * 2 – 7 / 3 % 2
step 1 3 * 2 = 6  → 7 + 6 – 7 / 3 % 2
step 2 7 / 3 = 2  → 7 + 6 – 2 % 2
step 3 2 % 2 = 0  → 7 + 6 – 0
step 4 7 + 6 = 13  → 13 – 0
step 5 13 – 0 = 13  → 13

Independent steps

Steps 1 and 2 do not depend on each other, so a compiler may do either one first. That is harmless here because neither has a side effect. The next lecture shows what happens when it is not harmless.

The program below performs exactly those five steps with temporary variables, so you can see every intermediate value:

#include <stdio.h>

int main(void)
{
    int a = 7, b = 3, c = 2;

    /* the whole expression in one line */
    int whole = a + b * c - a / b % c;

    /* the same expression, one operator at a time */
    int s1 = b * c;     /* 3 * 2      = 6  */
    int s2 = a / b;     /* 7 / 3      = 2  */
    int s3 = s2 % c;    /* 2 % 2      = 0  */
    int s4 = a + s1;    /* 7 + 6      = 13 */
    int s5 = s4 - s3;   /* 13 - 0     = 13 */

    printf("one-line result : %d\n", whole);
    printf("step 1 (b * c)  : %d\n", s1);
    printf("step 2 (a / b)  : %d\n", s2);
    printf("step 3 (s2 %% c) : %d\n", s3);
    printf("step 4 (a + s1) : %d\n", s4);
    printf("step 5 (s4 - s3): %d\n", s5);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e20_1.c -o e20_1 && ./e20_1
one-line result : 13
step 1 (b * c)  : 6
step 2 (a / b)  : 2
step 3 (s2 % c) : 0
step 4 (a + s1) : 13
step 5 (s4 - s3): 13

The one-line result and the step-by-step result agree, which is the whole point: your hand method and the compiler follow the same rules.

Worked Example 2: Mixing Arithmetic, Relational and Logical Operators

Conditions in real firmware combine all three families. With x = 10, y = 4, z = 3 we evaluate four expressions. Arithmetic binds tightest, then relational, then equality, then &&, then ||.

#include <stdio.h>

int main(void)
{
    int x = 10, y = 4, z = 3;

    int r1 = x - y > z && y % z;       /* ((x - y) > z) && (y % z)   */
    int r2 = x > y == z;               /* (x > y) == z               */
    int r3 = x + y * z < 20 || z - 3;  /* ((x + (y * z)) < 20) || (z - 3) */
    int r4 = !x && y || z;             /* ((!x) && y) || z           */

    printf("r1 = %d\n", r1);
    printf("r2 = %d\n", r2);
    printf("r3 = %d\n", r3);
    printf("r4 = %d\n", r4);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e20_2.c -o e20_2 && ./e20_2
r1 = 1
r2 = 0
r3 = 0
r4 = 1
VariableExpressionGrouping the compiler usesValue
r1x - y > z && y % z((x - y) > z) && (y % z): (6 > 3) is 1, 4 % 3 is 1, 1 and 1 is 11
r2x > y == z(x > y) == z: relational (level 6) beats equality (level 7), so 1 == 3 is 00
r3x + y * z < 20 || z - 3((x + (y * z)) < 20) || (z - 3): 22 < 20 is 0, z – 3 is 0, so 00
r4!x && y || z((!x) && y) || z: !10 is 0, 0 and 4 is 0, 0 or 3 is 11

What the compiler said

GCC compiled this file with two -Wparentheses warnings: suggest parentheses around comparison in operand of '==' for r2 and suggest parentheses around '&&' within '||' for r4. Both expressions are legal and the values above are correct, but the compiler is telling you that a human reader will probably misread them. Add parentheses.

Worked Example 3: Numbering the Steps of a Longer Expression

Now a longer one with a = 6, b = 4, c = 9, d = 2, e = 17, f = 5, g = 18:

a * b - c / d >= e % f + g

There are three level 3 operators, two level 4 operators and one level 6 operator. The left and right sides of >= are independent until the relational operator finally joins them.

Evaluation tree: a * b – c / d >= e % f + g
1st a * b  → 24
2nd c / d  → 4
3rd e % f  → 2
4th 24 – 4 (left side)  → 20
5th 2 + 18 (right side)  → 20
6th 20 >= 20  → 1

The 1st, 2nd and 3rd steps may happen in any order, and the 4th and 5th are also independent. Only the 6th must come last, because it needs both sides. Here is the program that proves it:

#include <stdio.h>

int main(void)
{
    int a = 6, b = 4, c = 9, d = 2, e = 17, f = 5, g = 18;

    /*  a * b - c / d >= e % f + g  */
    int t1 = a * b;        /* 1st: a * b            */
    int t2 = c / d;        /* 2nd: c / d            */
    int t3 = e % f;        /* 3rd: e % f            */
    int t4 = t1 - t2;      /* 4th: left side  -     */
    int t5 = t3 + g;       /* 5th: right side +     */
    int t6 = t4 >= t5;     /* 6th: relational >=    */

    printf("1st  a * b      = %d\n", t1);
    printf("2nd  c / d      = %d\n", t2);
    printf("3rd  e %% f      = %d\n", t3);
    printf("4th  %d - %d    = %d\n", t1, t2, t4);
    printf("5th  %d + g     = %d\n", t3, t5);
    printf("6th  %d >= %d   = %d\n", t4, t5, t6);
    printf("direct          = %d\n", a * b - c / d >= e % f + g);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e20_4.c -o e20_4 && ./e20_4
1st  a * b      = 24
2nd  c / d      = 4
3rd  e % f      = 2
4th  24 - 4    = 20
5th  2 + g     = 20
6th  20 >= 20   = 1
direct          = 1

Notice the value 20 on both sides. The relational operator >= gives 1, not 0, because the operands are equal. Off-by-one boundaries like this are the classic place for wrong results, so always evaluate the boundary case by hand.

Embedded C Programming Case: The ADC Scaling Bug

A 12-bit ADC returns values from 0 to 4095, and the reference voltage is 3300 mV. The conversion is reading / 4095 * 3300, right? Evaluate it with the left-to-right rule and you will see why that gives zero for every reading below 4095.

Same operators, different order, different answer
wrong raw / 4095 * 3300  → (3000 / 4095) * 3300
step 1 3000 / 4095 = 0 (integer division)  → 0 * 3300
step 2 0 * 3300  → 0
right raw * 3300 / 4095  → (3000 * 3300) / 4095
step 1 3000 * 3300  → 9900000 / 4095
step 2 9900000 / 4095  → 2417
#include <stdio.h>
#include <stdint.h>

/* Convert a 12-bit ADC reading (0..4095) to millivolts for a 3.3 V reference */
int main(void)
{
    uint32_t raw = 3000;

    uint32_t mv_wrong   = raw / 4095 * 3300;          /* divides first  */
    uint32_t mv_right   = raw * 3300 / 4095;          /* multiplies first */
    uint32_t mv_rounded = (raw * 3300 + 4095 / 2) / 4095;

    printf("raw             : %u\n", (unsigned)raw);
    printf("raw / 4095 * 3300 : %u mV\n", (unsigned)mv_wrong);
    printf("raw * 3300 / 4095 : %u mV\n", (unsigned)mv_right);
    printf("rounded           : %u mV\n", (unsigned)mv_rounded);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e20_3.c -o e20_3 && ./e20_3
raw             : 3000
raw / 4095 * 3300 : 0 mV
raw * 3300 / 4095 : 2417 mV
rounded           : 2418 mV

Multiplying first keeps the precision that integer division would throw away. The rounded version adds half the divisor before dividing, which turns truncation into round-to-nearest and gives 2418 mV instead of 2417.

Mind the integer width

The value 3000 * 3300 is 9,900,000. That fits in a 32-bit integer, but on 8-bit and 16-bit microcontrollers where int is 16 bits it overflows. The example uses uint32_t so the arithmetic is done in 32 bits on every target.

Common Mistakes and Troubleshooting

MistakeWhy it happensFix
Reading a / b * c as a / (b * c)Same level, left to right, so division goes firstWrite the parentheses you mean
Expecting x > y == z to compare three valuesRelational binds tighter than equalitySplit into two comparisons with &&
Dividing before multiplying in scaling codeInteger division truncates earlyMultiply first, using a wide enough type
Ignoring -WparenthesesWarning looks cosmeticTreat it as a readability bug report
Evaluating only the example valuesBoundary values hide bugsTest equal, zero and maximum inputs

Best Practices, Performance and Safety

  • Add parentheses whenever two different operator families meet. They cost nothing at run time because the compiler folds them away.
  • Break a long expression into named temporaries while debugging, exactly as e20_1.c does, then compact it again once it is trusted.
  • Compile with -Wall -Wextra and read every warning. They are free code review.
  • In scaling arithmetic, multiply first and divide last, and use fixed-width types such as uint32_t to avoid overflow on small targets.
  • Remember that unsigned arithmetic wraps silently while signed overflow is undefined behaviour. Both are security-relevant when the value sizes a buffer or a timeout.

Key Takeaways From This Free C Programming Course Lecture

  • Precedence decides who grabs operands first, associativity breaks ties.
  • Evaluate by substituting values, resolving parentheses, then repeatedly replacing the highest-precedence operator with its result.
  • Independent sub-expressions can run in any order, which is safe only when they have no side effects.
  • Relational operators bind tighter than equality, and equality tighter than && and ||.
  • In integer scaling, a * b / c and a / c * b are very different programs.

You can now take any expression from a code review or an exam and evaluate it with confidence. The next lecture in this free C programming course asks a sharper question: what if two operands have side effects? In which order does C evaluate them?

Practice Questions

Q1. With a = 9, b = 2, c = 4, what is a % b + c * 2 - a / c?

Level 3 first: 9 % 2 is 1, 4 * 2 is 8, 9 / 4 is 2. Then 1 + 8 – 2 is 7.

Q2. With p = 5, q = 5, r = 0, what is p == q || r && p > 9?

&& binds tighter than ||. So r && (p > 9) is 0, and (p == q) || 0 is 1. The answer is 1.

Q3. Why does 1000 / 3 * 3 not equal 1000?

Left to right: 1000 / 3 is 333 in integer arithmetic, then 333 * 3 is 999.

Interview Questions on Expression Evaluation

What is the difference between precedence and associativity?

Precedence ranks different operators against each other. Associativity decides the grouping when operators of the same precedence meet, for example a - b - c is (a - b) - c.

Why is multiply-then-divide preferred in fixed-point firmware?

Integer division discards the fractional part. Multiplying first preserves resolution, provided the intermediate product fits in the type.

Does adding parentheses slow the program down?

No. Parentheses only affect parsing. The generated machine code is identical to the version that relies on default precedence when the grouping is the same.

Conclusion

Evaluating expressions by hand is not an exam trick. It is a debugging technique. When firmware misbehaves, writing the evaluation ladder for the failing line often finds the bug faster than a debugger. You have practised it on arithmetic, on mixed relational and logical conditions, and on a real ADC scaling problem, and you verified every claim by compiling the code with two compilers. Keep following this free C programming course in order, because the operators you now understand are the building blocks of every control-flow statement that comes next.

For deeper reading, look up the operator precedence page on cppreference.com and the expressions chapter of the ISO C standard draft. Both agree with every result shown above.

Frequently Asked Questions

How do I evaluate a complex expression in C by hand?

Substitute the variable values, resolve parentheses from the inside out, then repeatedly evaluate the highest-precedence operator, using associativity on ties, until one value remains.

Is there a free C programming course for embedded systems?

Yes. This free C programming course from EmbeddedPathashala teaches C with a focus on embedded use cases such as ADC scaling, flags and registers, with compiled examples in every lecture.

Why does raw / 4095 * 3300 return 0?

Division and multiplication share a precedence level and associate left to right, so raw / 4095 runs first. In integer arithmetic it truncates to 0 for any raw below 4095, and 0 times 3300 is 0.

Does C always evaluate left to right?

No. Associativity only decides grouping. The order in which independent operands are evaluated is unspecified, which matters only when the operands have side effects.

Do parentheses change the speed of my embedded C program?

No. They affect how the compiler parses the expression, not the machine code, when the grouping is the same.

Which compiler flags help catch precedence mistakes?

Use -Wall -Wextra. GCC reports -Wparentheses for confusing combinations, and Clang has similar diagnostics.

Topics Covered

free C programming coursefree C tutorialC programming for embedded systemsembedded C programmingfree embedded C courseoperator precedenceexpression evaluationassociativity

Keep Going With the Course

Next we look at the order in which C evaluates operands, and why side effects make it dangerous.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →