๐งฎ 7. Expressions
7.1 What Is an Expression?
An expression is a combination of values, variables, operators, function calls, and parentheses that C can evaluate to produce a value.
a, 10, or price.
+, *, &&.
total = price + tax;
price + tax is an expression. The assignment
total = price + tax is also an expression in C.
7.2 Expression vs Statement
These two terms are related but not identical. An expression produces a value or performs an operation; a statement is a complete instruction that controls program execution or performs an action.
| Example | What it is | Idea |
|---|---|---|
a + b |
Expression | Produces a value |
a = b + 2 |
Expression | Assigns a value and itself has a value |
a = b + 2; |
Expression statement | The expression is used as a complete statement |
if (a > b) { ... } |
Selection statement | Controls which code executes |
7.3 Main Types of Expressions
a + b, a * b, a % b
a > b, a == b
a > 0 && b > 0
x = 10, x += 5
max = a > b ? a : b
(a = 2, b = 3)
7.4 Arithmetic Expressions
Arithmetic expressions use numeric operands and arithmetic operators.
int a = 10;
int b = 3;
printf("%d\n", a + b);
printf("%d\n", a - b);
printf("%d\n", a * b);
printf("%d\n", a / b);
printf("%d\n", a % b);
10 + 3 = 13
10 - 3 = 7
10 ร 3 = 30
10 / 3 = 3 (integer division)
10 % 3 = 1
7.5 Relational Expressions
Relational operators compare values. The result used by C for a
comparison is an int: 1 represents true
and 0 represents false.
| Operator | Meaning | Example |
|---|---|---|
< | Less than | a < b |
<= | Less than or equal | a <= b |
> | Greater than | a > b |
>= | Greater than or equal | a >= b |
== | Equal to | a == b |
!= | Not equal to | a != b |
int a = 10;
int b = 20;
printf("%d", a < b);
= means assignment, while == means comparison.
7.6 Logical Expressions
Logical operators combine or modify conditions.
| Operator | Meaning | Example |
|---|---|---|
&& | Logical AND | age >= 18 && age <= 60 |
|| | Logical OR | marks < 40 || attendance < 75 |
! | Logical NOT | !(x == 0) |
int age = 25;
int eligible = age >= 18 && age <= 60;
printf("%d", eligible);
7.7 Short-Circuit Evaluation
C uses short-circuit evaluation for &&
and ||. The right-hand operand may not be evaluated when
its result is already determined by the left-hand operand.
&& โ AND
If the left side is false, the complete result must be false, so the right side is skipped.
|| โ OR
If the left side is true, the complete result must be true, so the right side is skipped.
int x = 0;
if (x != 0 && 10 / x > 2)
{
printf("Yes");
}
x != 0 is false, the right side is not evaluated.
This is a common safe pattern for protecting a later operation.
7.8 Assignment Expressions
Assignment stores a value in an object. The assignment expression itself also has the value that was assigned.
int a;
int b;
b = (a = 10);
a = 10 โ assigns 10 to a โ expression value is 10b = 10 โ assigns 10 to b
Assignment can therefore be chained, but separate assignments are often easier to read for beginners.
7.9 Compound Assignment Expressions
Compound assignment combines an operation with assignment.
| Short form | Conceptual equivalent |
|---|---|
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 |
7.10 Mixed Expressions
A mixed expression contains multiple operators, so C must determine how the expression is grouped.
int result = 10 + 5 * 2;
7.11 Operator Precedence โ The Grouping Rules
Precedence determines which operator binds more strongly when an expression contains different operator levels.
| Level | Important operators | General category |
|---|---|---|
| High | (), function call, postfix ++ -- | Postfix / grouping |
prefix ++ -- + - ! ~, cast | Unary | |
* / % | Multiplicative | |
+ - | Additive | |
<< >> | Shift | |
< <= > >= | Relational | |
== != | Equality | |
&, ^, | | Bitwise | |
&&, || | Logical | |
?: | Conditional | |
| Low | = += -= *= /= %= and related assignments | Assignment |
7.12 Parentheses โ Make the Intent Obvious
int a = 10 + 5 * 2;
int b = (10 + 5) * 2;
| Expression | Grouping | Result |
|---|---|---|
10 + 5 * 2 | 10 + (5 * 2) | 20 |
(10 + 5) * 2 | (10 + 5) * 2 | 30 |
7.13 Associativity
When operators have the same precedence, their associativity determines how they are grouped.
int result = 20 - 5 - 3;
(20 - 5) - 3= 15 - 3
= 12
int result = 20 / 5 * 2;
(20 / 5) * 2= 4 * 2
= 8
7.14 Integer Division
int a = 5;
int b = 2;
int result = a / b;
printf("%d", result);
Since both operands are integers, integer division is performed. The result is an integer; the fractional part is not retained.
7.15 Floating-Point Division
int a = 5;
int b = 2;
double result = (double)a / b;
printf("%.2f", result);
| Expression | Division performed | Result |
|---|---|---|
5 / 2 |
Integer division | 2 |
(double)5 / 2 |
Floating-point division | 2.5 |
5.0 / 2 |
Floating-point division | 2.5 |
7.16 Type Conversion in Expressions
C may convert operands to compatible types during expression evaluation. Conversions can be implicit or explicit.
Implicit Conversion
int count = 10;
double price = 2.5;
double total = count * price;
The integer operand is converted to a compatible floating-point type for the multiplication.
Explicit Conversion โ Cast
int marks = 85;
int total = 100;
double percentage =
(double)marks / total * 100;
(double)marks โ convert marks for this expressionโ floating-point division
โ multiply by 100
7.17 Character Expressions
A character constant such as 'A' participates in integer
expressions because character types have integer representations.
char ch = 'A';
printf("%d", ch);
The exact numeric value comes from the execution character set.
On an ASCII-based system, 'A' has value 65.
char ch = 'A';
printf("%c\n", ch);
printf("%c\n", ch + 1);
B
7.18 Truth Values in Expressions
In a C condition, 0 is false and any nonzero value is true.
int x = -5;
if (x)
{
printf("True");
}
The value does not need to be exactly 1 to be true.
7.19 Conditional Operator ?:
The conditional operator is a compact expression for choosing between two values.
condition ? value_if_true : value_if_false
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
if-else statement. Use if-else
when the logic becomes difficult to read as one expression.
7.20 Pre-Increment and Post-Increment
Both ++a and a++ increase a
by one. The important difference is the value produced by the
expression.
| Form | Value produced | Then |
|---|---|---|
++a |
New value | a is incremented first |
a++ |
Old value | a is incremented after its value is used |
int a = 5;
int b = ++a;
a becomes 6 โ b receives 6
int a = 5;
int b = a++;
b receives 5 โ a becomes 6
7.21 Pre-Decrement and Post-Decrement
int a = 5;
int b = --a;
a becomes 4 โ b receives 4
int a = 5;
int b = a--;
b receives 5 โ a becomes 4
7.22 The Comma Operator
The comma operator evaluates its left operand, then its right operand. The value of the complete comma expression is the value of the rightmost expression.
int a, b;
int result = (a = 10, b = 20, a + b);
a = 10 โ then b = 20 โ then a + bFinal expression value = 30
7.23 Expression Evaluation โ A Worked Example
int result = 10 + 20 / 5 * 2 - 3;
7.24 Evaluation Order vs Precedence โ Critical C Concept
This is one of the most important ideas in C expressions: precedence is about grouping; evaluation order is a separate issue.
| Concept | Question it answers |
|---|---|
| Precedence | Which operator groups with its operands first? |
| Associativity | How are operators of the same precedence grouped? |
| Evaluation order | When are the individual operands or side effects evaluated? |
int x = f() + g();
The + operator tells us how the expression is grouped,
but it does not mean you should assume that f()
necessarily executes before g().
When the order matters, write separate statements so the intended sequencing is explicit.
7.25 Undefined Behavior from Unsafe Side Effects
Avoid modifying the same scalar object multiple times in one expression when the operations are not safely sequenced.
i++ + ++i
i are not safely sequenced relative to each other.
Prefer clear steps:
i++;
i++;
int result = i;
This is easier to read, easier to debug, and avoids depending on unsafe evaluation behavior.
7.26 Common Expression Mistakes
= vs ==
Assignment and equality comparison are different operations.
5 / 2 gives 2 when both operands are integers.
7.27 Quick Revision
๐ Parentheses can explicitly control grouping.
๐ * / % generally have higher precedence than + -.
๐ Operators with equal precedence follow their specified associativity.
๐ Integer รท Integer โ Integer division.
๐ Cast one operand when floating-point division is required.
๐ Zero โ false.
๐ Nonzero โ true.
7.28 Quick MCQs
Select an answer first, then click Check Answer. A correct choice becomes green. If the choice is wrong, it becomes red and the correct option is shown in green. The correct answer and explanation appear below after checking.
Multiplication has higher precedence than addition. First 5 * 2 = 10, then 10 + 10 = 20.
With two int operands, integer division is performed. The fractional part is discarded, so the result is 2.
* has higher precedence than +, assignment, and logical OR.
Parentheses force 10 + 5 to be grouped first. 15 * 2 gives 30.
Post-increment yields the old value first, so b receives 5. The side effect then increments a to 6.
The cast converts 5 to double before division. Floating-point division is performed, so the mathematical value is 2.5.
7.29 ๐ฏ Practice Problems
Expressions are best learned by evaluating them yourself. Use ๐ป Solve It Yourself first, open Hint only when needed, and use Show Program after attempting the problem.
Problem 1: Write a C program to evaluate the expression 10 + 5 * 2 and print the result.
Input: No input.
Output: Print the value of the expression.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int result;
result = 10 + 5 * 2;
printf("Result = %d", result);
return 0;
}
Problem 2: Write a C program to evaluate the expression (10 + 5) * 2 and print the result.
Input: No input.
Output: Print the value of the expression.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int result;
result = (10 + 5) * 2;
printf("Result = %d", result);
return 0;
}
Problem 3: Write a C program to evaluate 20 / 5 * 2 and print the result.
Input: No input.
Output: Print the value of the expression.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int result;
result = 20 / 5 * 2;
printf("Result = %d", result);
return 0;
}
Problem 4: Read two integers and calculate their average as a floating-point value.
Input: Two integers a and b.
Output: Print the average rounded to two decimal places.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
double average;
scanf("%d %d", &a, &b);
average = (a + b) / 2.0;
printf("Average = %.2f", average);
return 0;
}
Problem 5: Read obtained marks and total marks and calculate the percentage using explicit type casting.
Input: Two integers: obtained marks and total marks, where total marks is greater than zero.
Output: Print the percentage rounded to two decimal places.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int obtained, total;
double percentage;
scanf("%d %d", &obtained, &total);
percentage = (double)obtained / total * 100.0;
printf("Percentage = %.2f", percentage);
return 0;
}
Problem 6: Predict the values of a and b for int a = 5; int b = ++a; by writing and running the C program.
Input: No input.
Output: Print the final values of a and b.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a = 5;
int b = ++a;
printf("a = %d, b = %d", a, b);
return 0;
}
Problem 7: Predict the values of a and b for int a = 5; int b = a++; by writing and running the C program.
Input: No input.
Output: Print the final values of a and b.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a = 5;
int b = a++;
printf("a = %d, b = %d", a, b);
return 0;
}
Problem 8: Read two integers and find the larger value using the conditional expression.
Input: Two integers a and b.
Output: Print the larger value.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
int larger;
scanf("%d %d", &a, &b);
larger = (a > b) ? a : b;
printf("Larger = %d", larger);
return 0;
}
Problem 9: Write a C program to evaluate 10 + 20 / 5 * 2 - 3.
Input: No input.
Output: Print the result.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int result;
result = 10 + 20 / 5 * 2 - 3;
printf("Result = %d", result);
return 0;
}
Problem 10: Write a program to demonstrate the difference between integer division and floating-point division.
Input: Two integers a and b, where b is not zero.
Output: Print both the integer quotient and floating-point quotient.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("Integer Division = %d
", a / b);
printf("Floating Division = %.2f", (double)a / b);
return 0;
}
7.30 Key Takeaway
Step 1: Check parentheses.
Step 2: Identify operator precedence.
Step 3: Apply associativity where necessary.
Step 4: Check data types.
Step 5: Check whether integer or floating-point arithmetic is being performed.
Step 6: Evaluate the expression step by step.
This method is extremely useful for C programming exams and coding interviews.
๐ฌ Expressions โ Step-by-Step Evaluation
Follow how C groups and evaluates
10 + 20 / 5 * 2 - 3
using precedence and associativity.
๐ฌ Expression Evaluation Visualizer
Watch each grouped operation become active as the expression is reduced to its final value.
10 + 20 / 5 * 2 - 3
20 / 5 = 4
4 * 2 = 8
10 + 8 = 18
18 - 3 = 15
๐ Program Tracing โ Expressions
Trace arithmetic, logical, and conditional expressions using actual C variables and outputs.
โ
๐ค Expressions โ Interview Questions
๐ก Expressions โ Extra Tips
- For output-prediction questions, identify parentheses, then precedence, then associativity.
-
Always check the data types
before evaluating division.
5 / 2and5.0 / 2do not produce the same mathematical value. - Remember that precedence determines grouping, not the general evaluation order of independent operands.
- Be very careful when an expression contains pre/post increment. First determine which value the expression yields.
- Avoid clever expressions that modify the same object multiple times without clear sequencing. Split them into simple statements.
- In interviews, explain an expression step by step instead of only giving the final answer.
โ๏ธ Expressions โ Extra Practice Questions
-
Evaluate
8 + 12 / 3 * 2 - 1step by step. -
Compare the results of
7 / 2and(double)7 / 2. -
Explain the grouping of
20 - 5 - 3. -
Predict the values after
int a = 4; int b = ++a + 2;. - Rewrite a complicated mixed expression using parentheses so the intended grouping is obvious.
-
Explain why precedence alone is not enough
to justify the behavior of
i++ + ++i.