Module I · Basics of C · Lecture 6
🎯 Why This Matters
Every calculation a C program ever performs — computing a bill total, converting a temperature, working out simple interest — comes down to combining variables and values using operators. You’ve already used a few without naming them: the = in int age = 21;, the + in Sum = Sum + Counter. This post makes that knowledge formal and complete, covering exactly how C’s assignment, unary, and arithmetic operators work — including the two places beginners most often get surprised: operator precedence and the difference between x++ and ++x.
➕ Arithmetic Operators
C provides five arithmetic operators, all working on two operands (values on either side):
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3 (integer division) |
% | Modulus (remainder) | 7 % 2 | 1 |
#include <stdio.h>
int main() {
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}
🖥️ Output:
a + b = 22
a – b = 12
a * b = 85
a / b = 3
a % b = 2
💡 Two details worth remembering: Notice %% is used inside the format string to print a literal % sign — a plain % would otherwise be read as the start of a specifier. And as covered in the “Data Types in C” post, a / b performs integer division whenever both operands are integers — the decimal part is simply discarded, not rounded.
The modulus operator (%) only works with integers, and it’s genuinely useful far beyond “the remainder” — it’s the standard way to test divisibility (as in the odd/even checker from the Algorithms post), extract the last digit of a number, or wrap a value around within a fixed range.
⚡ Unary Operators
Unlike arithmetic operators, a unary operator acts on just one operand. C gives you unary plus and minus, plus the two operators that trip up almost every beginner at least once: increment and decrement.
Unary Plus and Minus
#include <stdio.h>
int main() {
int x = 8;
printf("+x = %d\n", +x);
printf("-x = %d\n", -x);
printf("-(-x) = %d\n", -(-x));
return 0;
}
🖥️ Output:
+x = 8
-x = -8
-(-x) = 8
Unary + doesn’t actually change anything — it exists mostly for symmetry with unary -, which flips a value’s sign. Applying it twice, as in -(-x), flips the sign back.
Increment and Decrement
++ adds 1 to a variable; -- subtracts 1. Both can appear before the variable (pre) or after it (post) — and that position genuinely changes what an expression evaluates to.

#include <stdio.h>
int main() {
int x = 5, y;
y = x++; // post-increment
printf("Post: x = %d, y = %d\n", x, y);
x = 5; // reset
y = ++x; // pre-increment
printf("Pre: x = %d, y = %d\n", x, y);
return 0;
}
🖥️ Output:
Post: x = 6, y = 5
Pre: x = 6, y = 6

In both cases, x ends up as 6 — incrementing always happens. The difference is entirely about what value gets handed to y at the moment of assignment: post-increment (x++) hands over the old value first and increments afterward; pre-increment (++x) increments first and hands over the new value. This exact distinction shows up constantly in loop counters and array indexing later in this course, so it’s worth tracing by hand until it feels automatic.
✍️ Assignment Operators
You already know the plain =. C also provides five compound assignment operators that combine an arithmetic operation with assignment in one step.
| Operator | Equivalent to |
|---|---|
x += 5 | x = x + 5 |
x -= 5 | x = x - 5 |
x *= 5 | x = x * 5 |
x /= 5 | x = x / 5 |
x %= 5 | x = x % 5 |

#include <stdio.h>
int main() {
int total = 100;
total += 50; // total = total + 50
printf("After +=: %d\n", total);
total -= 30;
printf("After -=: %d\n", total);
total *= 2;
printf("After *=: %d\n", total);
total /= 4;
printf("After /=: %d\n", total);
return 0;
}
🖥️ Output:
After +=: 150
After -=: 120
After *=: 240
After /=: 60
Chained Assignment
C also allows assigning the same value to several variables in a single statement:
#include <stdio.h>
int main() {
int a, b, c;
a = b = c = 10;
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
🖥️ Output:
a = 10, b = 10, c = 10
This works because assignment itself is an expression that evaluates to the value being assigned — c = 10 evaluates to 10, which then gets assigned to b, and so on. Assignment groups right to left, which is exactly why this chains the way it does.
📐 Operator Precedence and Associativity
When an expression mixes several operators, C needs rules to decide which one runs first. Precedence decides which operator type takes priority; associativity decides the direction of evaluation when two operators of the same precedence sit side by side.

#include <stdio.h>
int main() {
int result = 2 + 3 * 4;
printf("2 + 3 * 4 = %d\n", result);
int trickier = (2 + 3) * 4;
printf("(2 + 3) * 4 = %d\n", trickier);
return 0;
}
🖥️ Output:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Even though addition is written first, * has higher precedence than +, so 3 * 4 is evaluated before the addition — exactly like the order of operations you learned in school math. Parentheses always override precedence, which is why wrapping 2 + 3 in the second example forces it to happen first, changing the result entirely.
A Trickier Example — Mixing Everything
#include <stdio.h>
int main() {
int a = 5;
int result = a++ + ++a;
printf("result = %d, a = %d\n", result, a);
return 0;
}
🖥️ Output (on most common compilers):
result = 12, a = 7
⚠️ A genuine warning, not just a fun fact: Expressions like this one are technically undefined behavior in C — the language doesn’t guarantee the order in which a++ and ++a are evaluated within the same statement, so different compilers are allowed to produce different results. This example exists to show you why professional C code avoids modifying the same variable more than once in a single expression — not to encourage writing code like it.
🧮 Worked Activity 1: Simple Interest Calculator
Let’s put arithmetic operators to work on this lecture’s proposed activity — calculating simple interest, using the formula SI = (P × R × T) / 100.
#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest (%%): ");
scanf("%f", &rate);
printf("Enter time (in years): ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
printf("\nSimple Interest = %.2f\n", simpleInterest);
printf("Total Amount = %.2f\n", principal + simpleInterest);
return 0;
}
🖥️ Sample run:
Enter principal amount: 10000
Enter rate of interest (%): 5
Enter time (in years): 2
Simple Interest = 1000.00
Total Amount = 11000.00
Notice all three inputs are declared as float, even though rate and time are often whole numbers in practice — this ensures the multiplication and division happen in decimal arithmetic from the start, avoiding the integer-division trap covered in the Data Types post.
📊 Worked Activity 2: Average of N Numbers
The second half of this lecture’s activity — computing an average — is a perfect place to combine compound assignment with the increment operator.
#include <stdio.h>
int main() {
int n;
float mark, sum = 0;
printf("How many subjects? ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("Enter marks for subject %d: ", i);
scanf("%f", &mark);
sum += mark; // shorthand for: sum = sum + mark
}
float average = sum / n;
printf("\nTotal = %.2f\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
🖥️ Sample run:
How many subjects? 3
Enter marks for subject 1: 78
Enter marks for subject 2: 85
Enter marks for subject 3: 91
Total = 254.00
Average = 84.67
Two operators from this post are doing real work here: i++ advances the loop counter each pass (post-increment, since the loop doesn’t need the value returned — just the side effect), and sum += mark accumulates the running total — the exact same accumulator pattern from the “Sum of First N Natural Numbers” algorithm, just written with C’s compound assignment shorthand instead of a full sum = sum + mark.
🚫 Common Mistakes Beginners Make
| Mistake | Why it happens |
|---|---|
Expecting 2 + 3 * 4 to evaluate left-to-right as 20 | * has higher precedence than +, regardless of writing order |
Using x++ and ++x interchangeably | They only behave identically as standalone statements — inside a larger expression, the value returned differs |
Writing a = b = c and expecting left-to-right assignment | Assignment associates right to left — c‘s value is assigned to b first, then that result to a |
Modifying the same variable twice in one expression (e.g. a++ + ++a) | The evaluation order is undefined by the C standard — different compilers may give different answers |
Forgetting that % only works on integers | Attempting 5.5 % 2 is a compile error — use fmod() from <math.h> for decimal remainders |
🎓 Practice Exercises
Exercise 1: Predict the Output
int a = 10;
printf("%d\n", a-- - --a);
(Answer: this is undefined behavior — the same warning as the a++ + ++a example. The takeaway is recognizing why it’s unsafe, not computing a specific number.)
Exercise 2: Trace the Precedence
int result = 10 - 2 * 3 + 4 / 2;
printf("%d\n", result);
(Answer: 6 — multiplication and division happen first, left to right: 2*3=6 and 4/2=2, giving 10 - 6 + 2 = 6.)
Exercise 3: Build It Yourself
Write a program that reads a shopping cart’s item price and quantity, then uses compound assignment to add a fixed ₹40 delivery fee to the total — printing the subtotal, the fee, and the final total separately.
❓ Frequently Asked Questions
Q: Is there an exponent (power) operator in C, like ** in some other languages?
No — C has no built-in exponent operator. You’d use the pow() function from <math.h> instead, e.g. pow(2, 3) for 2³.
Q: Why does C even allow undefined-behavior expressions like a++ + ++a to compile?
C prioritizes giving the compiler freedom to optimize aggressively over catching every possible ambiguity at compile time. It’s on the programmer to avoid writing expressions the standard doesn’t pin down — which is exactly why professional style guides ban modifying a variable more than once per statement.
Q: Do I need parentheses around (P * R * T) / 100 in the simple interest formula, or would it work without them?
Since * and / share the same precedence level and associate left-to-right, P * R * T / 100 would actually evaluate identically without the parentheses. They’re included in the example purely for readability — making the grouping obvious to a human reader, not because C requires it.
Q: What’s the difference between = and ==?= is the assignment operator covered in this post — it stores a value. == is a comparison (relational) operator, covered in the next post, that checks whether two values are equal. Mixing them up is one of the most common bugs in beginner C code.
✅ Key Takeaways
- C’s five arithmetic operators (
+ - * / %) behave as expected, except that/performs integer division between two integers, and%only works on integers. - Unary operators act on a single operand —
++and--are the ones to watch closely, since pre- and post-versions return different values even though both modify the variable identically. - Compound assignment operators (
+=,-=, etc.) are shorthand —x += 5andx = x + 5do exactly the same thing. - Precedence decides which operator runs first in a mixed expression; associativity decides direction when operators share the same precedence — assignment associates right-to-left, arithmetic left-to-right.
- Modifying the same variable more than once inside a single expression (like
a++ + ++a) is undefined behavior — real code should always avoid it, regardless of what any particular compiler happens to output.
🚀 Next up: With arithmetic, unary, and assignment operators covered, we’ll move on to relational and logical operators — how C compares values with
==,>, and friends, and how&&,||, and!combine multiple conditions into one.