What is C Expression Tracing Practice- C Programming Course Online

🚀 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 Tracing Practice

Twelve solved exercises and five open questions, all compiled and checked

Ch 4
Operators and Expressions
12
Solved exercises
Beginner
Level

Reading about precedence is one thing. Predicting the output of a tricky snippet with a pencil is another. This lecture of our free C programming course is a guided workout: twelve solved exercises on unary operators, logical tests, the ternary operator, increments, modulus, compound assignment, the comma operator and type conversion.

Every program was compiled and run, so each answer you see is real output and not a guess. The exercises are the same kind of question that appears in written tests and in embedded C programming interviews. Use this free C tutorial the right way: cover the answer, trace the code on paper, then compare.

What You Will Learn in This Free C Programming Course Lecture

  • A repeatable tracing sheet for predicting program output
  • How unary minus and logical NOT interact with binary operators
  • How the ternary operator and short-circuit logic simplify to a single value
  • How modulus and division behave with negative numbers in modern C
  • How compound assignment and the comma operator evaluate from right to left and left to right
  • How integer division, casts and char arithmetic change the final value

Prerequisites for This Free C Programming Course Lecture

  • The expression evaluation and operand order lectures from this chapter
  • Basic printf format specifiers such as %d, %c and %f

A Tracing Sheet for C Expressions

Before the exercises, adopt a routine. Experts do not run the code in their head all at once. They fill in a small table with one line per statement and update the variables after each one.

StepWhat to doExample
1List every variable with its starting valuea = 6
2Rewrite the statement with parentheses showing the groupinga = (a - (-a)) + (!a)
3Evaluate unary operators, then level 3, then level 4, and so on-a is -6, !a is 0
4Perform the assignment last and update the variablea becomes 12
5Check the type: integer or floating, signed or unsignedall int

Set A: Unary, Relational, Logical and Ternary Exercises

These four exercises use values that make short-circuit evaluation and the conditional operator easy to see.

#include <stdio.h>

int main(void)
{
    /* Exercise 1: unary minus and logical NOT */
    int a = 6;
    a = a - -a + !a;
    printf("Ex1: a = %d\n", a);

    /* Exercise 2: relational result feeding a logical AND */
    int p = 3, q = 7, c, d;
    c = p > q;
    d = (p < q) && (c == 0);
    printf("Ex2: c = %d, d = %d\n", c, d);

    /* Exercise 3: NOT over a grouped condition, then a ternary */
    int h = 8, k = 5, m = 9, e, f;
    e = !(h < k && k < m);
    f = (h > m) ? h - m : m - h;
    printf("Ex3: e = %d, f = %d\n", e, f);

    /* Exercise 4: precedence of * over + */
    int n = 4;
    n = n + 3 * n;
    printf("Ex4: n = %d\n", n);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e22_1.c -o e22_1 && ./e22_1
Ex1: a = 12
Ex2: c = 0, d = 1
Ex3: e = 1, f = 1
Ex4: n = 16
Exercise 1 ladder: a = a – -a + !a, with a = 6
unary -a  → -6
unary !a (6 is true, so NOT gives 0)  → 0
level 4 6 – (-6)  → 12
level 4 12 + 0  → 12
assign a =  → 12
ExerciseHand traceResult
1Unary first: -a is -6 and !a is 0. Then 6 – (-6) = 12, 12 + 0 = 12a = 12
2c = p > q is 3 > 7 = 0. Then (3 < 7) is 1 and (c == 0) is 1, so 1 && 1 = 1c = 0, d = 1
3h < k is 8 < 5 = 0, so && skips the right side. !0 = 1. Then 8 > 9 is false, so f = 9 – 8 = 1e = 1, f = 1
4Multiply first: 3 * 4 = 12. Then 4 + 12 = 16. Reading n twice is fine because n is changed only once, by the assignmentn = 16

Set B: Increment, Modulus, Compound Assignment and Comma

These exercises are where beginners most often slip. Watch the direction of evaluation in each one.

#include <stdio.h>

int main(void)
{
    /* Exercise 5: prefix and postfix on two different variables */
    int u = 5, v = 5;
    printf("Ex5: %d %d\n", ++u, v--);
    printf("Ex5: %d %d\n", u, v);

    /* Exercise 6: modulus and division with a negative operand */
    int n = 23, a, b, c;
    a = n % -5;
    b = n / 4 % 3;
    c = -n / 4;
    printf("Ex6: a = %d, b = %d, c = %d\n", a, b, c);

    /* Exercise 7: compound assignment chain */
    int w = 3, x = 4, y = 2, z = 5;
    w *= x + 1;
    y += z *= 2;
    printf("Ex7: w = %d, y = %d, z = %d\n", w, y, z);

    /* Exercise 8: comma operator on the right of += */
    int i, j, l, total = 10;
    total += (i = 2, j = i * 3, l = i + j);
    printf("Ex8: i = %d, j = %d, l = %d, total = %d\n", i, j, l, total);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e22_2.c -o e22_2 && ./e22_2
Ex5: 6 5
Ex5: 6 4
Ex6: a = 3, b = 2, c = -5
Ex7: w = 15, y = 12, z = 10
Ex8: i = 2, j = 6, l = 8, total = 18
Exercise 7 ladder: y += z *= 2 (assignment is right to left)
start w = 3, x = 4, y = 2, z = 5  → 
first z *= 2 means z = 5 * 2  → z = 10
then y += z means y = 2 + 10  → y = 12
also w *= x + 1 means w = 3 * (4 + 1)  → w = 15
ExerciseHand traceResult
5++u gives 6, v-- gives the old value 5 and then v becomes 4. They touch different variables, so the code is well definedprints 6 5 then 6 4
6In C99 and later the sign of % follows the left operand and division truncates toward zero: 23 % -5 = 3, 23 / 4 % 3 = 5 % 3 = 2, -23 / 4 = -5a = 3, b = 2, c = -5
7The right side of a compound assignment is fully evaluated first, so w *= x + 1 is w = 3 * 5 = 15. Then y += z *= 2 evaluates right to leftw = 15, y = 12, z = 10
8The comma runs left to right: i = 2, j = 6, l = 8. The value of the parenthesised expression is 8, so total = 10 + 8total = 18

Why exercise 5 is safe

In the previous lecture we saw that modifying one variable twice in an expression is undefined. Here ++u and v-- change different variables, so there is nothing to conflict. The argument order is still unspecified, but it does not affect the values.

Set C: Types, Characters, Swapping and Maximum

The last set mixes integer division with floating point, treats a char as a small integer, and uses the comma operator and the conditional operator in practical idioms.

#include <stdio.h>

int main(void)
{
    /* Exercise 9: integer division versus a cast */
    float r;
    r = 17 / 4;
    printf("Ex9 : %f\t", r);
    r = (float)17 / 4 + (17 / 4);
    printf("%f\n", r);

    /* Exercise 10: a char takes part in int arithmetic */
    int a = 5;
    char ch = 'B';
    a = a + ch + 10;
    printf("Ex10: %d,%c\t%d,%c\n", ch, ch, a, a);

    /* Exercise 11: swap with the comma operator */
    int s = 3, t = 9, tmp;
    tmp = s, s = t, t = tmp;
    printf("Ex11: s = %d, t = %d\n", s, t);

    /* Exercise 12: maximum with the conditional operator */
    int max, lo = 12, hi = 31;
    max = lo > hi ? lo : hi;
    printf("Ex12: max = %d\n", max);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra e22_3.c -o e22_3 && ./e22_3
Ex9 : 4.000000	8.250000
Ex10: 66,B	81,Q
Ex11: s = 9, t = 3
Ex12: max = 31
ExerciseHand traceResult
917 / 4 is integer division, 4, stored as 4.0. Then (float)17 / 4 is 4.25 because the cast makes it floating point, and (17 / 4) adds the integer 44.000000 then 8.250000
10'B' is 66 in ASCII. a = 5 + 66 + 10 = 81, and 81 printed as a character is Q66,B and 81,Q
11The comma has the lowest precedence, so tmp = s, s = t, t = tmp is three separate assignments in orders = 9, t = 3
12lo > hi is 12 > 31, false, so the conditional operator selects himax = 31
Why the cast matters in exercise 9
no cast 17 / 4  → 4 (both integers)
cast (float)17 / 4  → 4.25 (float / int becomes float)
sum 4.25 + 4  → 8.25

ASCII assumption

Exercise 10 assumes the ASCII character set, where ‘B’ is 66. That holds on every mainstream embedded toolchain, but the C standard does not require it. Never depend on a specific character code in portable code.

Try It Yourself: Five Unsolved Questions

Trace each on paper first, then open the answer. All five were compiled and checked.

Q1. int a = 5, b = 2; int r = a / b * b + a % b; What is r?

a / b is 2, times b is 4, plus a % b which is 1. r = 5. Notice that this recovers the original a, which is the division identity.

Q2. int x = 3; x += x * 2 + 1; What is x?

The right side is evaluated fully first: 3 * 2 + 1 = 7. Then x = 3 + 7. x = 10.

Q3. int p = 4, q = 9; int r = (p > q) ? p : (q > 8 ? 100 : 200); What is r?

p > q is false, so the second branch is chosen. q > 8 is true, so r = 100.

Q4. int i = 2, j = 3, k; k = (i++, j + i); What are k and i?

The comma guarantees i++ completes first, so i is 3. Then j + i = 6. k = 6, i = 3.

Q5. float f = 7 / 2 * 2.0f; What is f?

Left to right: 7 / 2 is integer division, 3. Then 3 * 2.0f is 6.0. f = 6.0, not 7.0.

Common Mistakes When Tracing Expressions

MistakeCorrection
Applying ! to the whole condition instead of the nearest operand! has level 2 and binds only to the operand next to it, so use parentheses for a group
Evaluating a compound assignment left to rightAssignments are right to left. Evaluate the rightmost one first
Forgetting that x++ yields the old valuePostfix gives the old value, prefix gives the new one
Dividing negative numbers by feelSince C99, division truncates toward zero and the remainder takes the sign of the dividend
Assuming a cast applies to the whole expressionA cast is a unary operator and applies only to the operand next to it

Best Practices From These Exercises

  • Write tracing tables in embedded C programming code reviews. They find bugs faster than arguing.
  • Avoid clever single-line expressions in firmware. A swap with the comma operator is a puzzle, a swap with three lines is maintainable.
  • Use explicit casts when mixing integer and floating-point values, and comment why.
  • Run the code. Hand tracing builds intuition, but the compiler is the final authority.

Key Takeaways From This Free C Programming Course Lecture

  • Fill in a tracing table instead of evaluating everything in your head.
  • Unary operators bind first, assignment last, and compound assignment runs right to left.
  • The comma operator guarantees left-to-right order and returns its last value.
  • Integer division truncates, and a cast changes which division is performed.
  • Modifying different variables in one expression is fine. Modifying the same one twice is not.

Interview Questions

What does a = b = c = 4 do?

Assignment is right to left, so c becomes 4, then b becomes the value of that assignment, 4, then a becomes 4.

What is the value of -7 / 2 and -7 % 2 in C99 and later?

-3 and -1. Division truncates toward zero and the remainder has the sign of the dividend.

How would you swap two integers without the comma operator?

Use a temporary variable in three statements. It is clearer and just as fast.

Conclusion

Twelve exercises, five open questions and one habit: write the trace down. If you can fill in a tracing table correctly for every exercise in this lecture, you are ready for any evaluation question an interviewer asks and, more importantly, for reading other people’s firmware. The next lecture of this free C programming course looks at the other side of the coin: code that looks plausible but does not even compile, or compiles with undefined behaviour.

Frequently Asked Questions

How do I practise predicting C program output?

Write a tracing table with each variable’s value after every statement, rewrite expressions with explicit parentheses, then compile and run the code to check yourself.

Where can I find a free C programming course with solved exercises?

This free C programming course from EmbeddedPathashala includes solved, compiled exercises in every chapter, aimed at embedded C programming.

What is the result of a % b when a is negative?

Since C99 the result takes the sign of the dividend, so -7 % 2 is -1 and 23 % -5 is 3.

Why does 7 / 2 * 2.0f give 6.0 and not 7.0?

Operators of equal precedence group left to right, so 7 / 2 is integer division giving 3 before the multiplication converts to float.

Is the comma operator useful in embedded C?

Occasionally, in for-loop headers with two counters. Elsewhere separate statements are clearer.

What does the result of an assignment expression equal?

The assigned value, which is why a = b = 4 works and why total += (i = 2, …) uses the last comma operand.

Topics Covered

free C programming coursefree C tutorialC programming for embedded systemsembedded C programmingfree embedded C courseC interview questionsoperator practicecompound assignment

Keep Going With the Course

Next we look at expressions that do not compile, and the ones that compile but are undefined.

💬 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 →