C Programming Operators,
Expressions & Preprocessor
🎯 Learning Objectives
By the end of this lecture, students will be able to:
Understand different types of operators in C.
Learn how precedence and evaluation order work in expressions.
Explore increment/decrement operators and conditional operator
usage.
Know how initialization works in C programs.
Get introduced to the C Preprocessor and its role in compilation.
1️⃣ Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator Meaning Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 7 * 2 14
/ Division 10 / 3 3 (integer division)
% Modulus (remainder) 10 % 3 1
🔑 Note:
If both operands are integers, division result is also integer.
% only works with integers.
2️⃣ Relational and Logical Operators
(a) Relational Operators
Used to compare two values. Result is either true (1) or false (0).
Operator Meaning Example Result
< Less than 5 < 10 1
> Greater than 5 > 10 0
== Equal to 5 == 5 1
!= Not equal to 5 != 3 1
<= Less than or equal to 3 <= 3 1
>= Greater than or equal to 7 >= 10 0
(b) Logical Operators
Logical operators are used to combine or modify conditions
in decision-making statements like if, while, for, etc.
They always produce a result of 1 (true) or 0 (false).
Operator Meaning Example Result
&& Logical AND (5 > 2) && (10 > 5) 1
|| Logical OR 5 > 2 || 3 > 5 1
! Logical NOT !(5 > 2) 0
3️⃣ Increment and Decrement Operators
Operator Meaning Example Explanation
++ Increment by 1 x++ or ++x Adds 1
-- Decrement by 1 x-- or --x Subtracts 1
🔑 Prefix vs Postfix
++x (prefix) → increments before use.
1️⃣ Prefix (++x / --x)
Meaning: Increment (or decrement) the variable before
using it in an expression.
Steps:
1. Increment the variable value.
2. Use the new value in the expression.
Example – Prefix Increment
#include <stdio.h>
int main() {
int x = 5;
int y;
y = ++x; // increment x first, then assign
printf("x = %d, y = %d\n", x, y);
return 0;
}
✅ Output:
x = 6, y = 6
📌 Explanation:
1. ++x → x becomes 6 before assigning to y
2. y gets the new value of x → y = 6
x++ (postfix) → increments after use.
2️⃣ Postfix (x++ / x--)
Meaning: Increment (or decrement) the variable after
using it in an expression.
Steps:
1. Use the current value in the expression.
2. Then increment the variable.
🔹 Example – Postfix Increment
#include <stdio.h>
int main() {
int x = 5;
int y;
y = x++; // assign first, then increment
printf("x = %d, y = %d\n", x, y);
return 0;
✅ Output:
x = 6, y = 5
📌 Explanation:
x++ → y gets current value of x → y = 5
Then x is incremented → x = 6
3️⃣ Key Differences
Feature Prefix (++x) Postfix (x++)
Operation order Increment before using Increment after using
Expression
Returns new value Returns original value
value
Use in y = ++x → y gets y = x++ → y gets original
assignment incremented value value
Commonly used in for/while Also used, but careful with
Use in loops
loops expressions
🔹 Summary
++x→ prefix → increment first, then use.
x++→ postfix → use first, then increment.
Works the same for decrement (--x and x--).
Commonly used in loops, counters, and expressions.
4️⃣ Assignment Operators
Operator Example Equivalent To Explanation
+= x += 5 x = x + 5 Adds 5 to current value of x
-= x -= 5 x = x - 5 Subtracts 5 from current value of x
*= x *= 5 x = x * 5 Multiplies x by 5
/= x /= 5 x = x / 5 Divides x by 5
%= x %= 5 x = x % 5 Stores remainder of x ÷ 5
🔹 Example – Using Compound Assignment
#include <stdio.h>
int main() {
int x = 10;
x += 5; // x = x + 5 → 15
printf("After x += 5: %d\n", x);
x -= 3; // x = x - 3 → 12
printf("After x -= 3: %d\n", x);
x *= 2; // x = x * 2 → 24
printf("After x *= 2: %d\n", x);
x /= 4; // x = x / 4 → 6
printf("After x /= 4: %d\n", x);
x %= 5; // x = x % 5 → 1
printf("After x %%= 5: %d\n", x);
return 0;
}
✅ Output:
After x += 5: 15
After x -= 3: 12
After x *= 2: 24
After x /= 4: 6
After x %= 5: 1
📌 Explanation:
1. x += 5 → adds 5 to 10 → x = 15
2. x -= 3 → subtracts 3 → x = 12
3. x *= 2 → multiplies by 2 → x = 24
4. x /= 4 → divides by 4 → x = 6
5. x %= 5 → remainder of 6 ÷ 5 → x =
Key Points for Students
Assignment operators store values in variables.
Compound assignments make code shorter and easier to
read.
Can be used with all arithmetic operators (+, -, *, /, %).
%= operator works only with integers.
5️⃣ Conditional (Ternary) Operator
Shortcut for if-else.
The ternary operator is a shortcut for if-else statements.
It evaluates a condition and returns one value if true and another
value if false.
Called “ternary” because it uses three parts: condition, true-
expression, false-expression.
1️⃣ Syntax
condition ? expression1 : expression2;
condition → expression that evaluates to true (1) or false (0)
expression1 → executed if condition is true
expression2 → executed if condition is false
2️⃣ Example – Find Maximum of Two Numbers
#include <stdio.h>
int main() {
int x = 10, y = 20;
int max = (x > y) ? x : y; // if x>y true → max=x,
else max=y
printf("Maximum = %d\n", max);
return 0;
}
✅ Output:
Maximum = 20
📌 Explanation:
1. Condition: x > y → 10 > 20 → false
2. Since false → expression2 is chosen → max = y → 20
6️⃣ Expressions, Precedence and Order of
Evaluation
1️⃣ What is an Expression?
An expression is a combination of constants, variables, operators,
and function calls that produces a value.
🔹 Example:
int x = 5, y = 10;
int z = x + y * 2;
Here, x + y * 2 is an expression.
It combines variables (x, y), operator (+, *), and constants (2).
The result is stored in z.
2️⃣ Operator Precedence
Operator precedence determines which operator is evaluated first in
an expression.
🔹 Example:
int result = 2 + 3 * 4;
* has higher precedence than +
So, 3 * 4 = 12 is evaluated first
Then 2 + 12 = 14 → result = 14
🔹 Precedence Table (High → Low)
Precedence Operators Example
1 ++, --, ! ++x, !flag
2 *, /, % x * y, x / y
3 +, - x + y, x - y
4 <, >, <=, >= x < y, x >= y
5 ==, != x == y, x != y
6 && a && b
7 `
8 ?: (ternary) (x>y) ? x : y
9 =, +=, -=, *=, /=, %= x = 10, x += 5
3️⃣ Order of Evaluation (Associativity)
Operators with the same precedence are evaluated
according to associativity.
Left-to-right → evaluated from left side first
Right-to-left → evaluated from right side first
🔹 Examples:
1. Left-to-right
int x = 10 - 5 - 2; // left-to-right
// (10 - 5) - 2 = 5 - 2 = 3
2. Right-to-left
int x;
x = y = 5; // assignment is right-to-left
// y = 5 → x = y → x = 5
4️⃣ Combined Example
#include <stdio.h>
int main() {
int x = 5, y = 10, z;
z = x + y * 2; // * has higher precedence → 10*2=20, then
5+20=25
printf("z = %d\n", z);
return 0;
}
✅ Output:
z = 25
7️⃣ Block Structure in C
A block is a group of statements enclosed in { }.
Helps in grouping multiple statements inside loops, conditionals,
or functions.
Example:
if (x > 0) {
printf("Positive\n");
printf("End of block\n");
}
8️⃣ Initialization in C
Assigning a value to a variable at the time of declaration.
Example:
int a = 10; // initialized
int b; // declared only
Always initialize variables to avoid garbage values.
9️⃣ C Preprocessor
The C Preprocessor processes code before compilation.
Common Preprocessor Directives:
1. #include → Includes header files.
2. #include <stdio.h>
3. #define → Defines constants/macros.
4. #define PI 3.14
5. #undef → Undefines a macro.
6. #ifdef / #ifndef → Conditional compilation.
📌 Example:
#include <stdio.h>
#define PI 3.14159
int main() {
float r = 5, area;
area = PI * r * r;
printf("Area = %.2f", area);
return 0;
}
📝 Quick Recap
Operators: Arithmetic, Relational, Logical, Assignment,
Increment/Decrement, Conditional.
Expressions depend on precedence and evaluation order.
Blocks {} group multiple statements.
Always initialize variables.
Preprocessor handles macros, header files, and conditional
compilation.