๐Ÿงฎ 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.

Operands Values being used, such as a, 10, or price.
Operators Symbols that perform an operation, such as +, *, &&.
Result The value produced after the expression is evaluated.
Example:
total = price + tax;

price + tax is an expression. The assignment total = price + tax is also an expression in C.

Expression Thinking
1
Operands
What values are involved?
โ†’
2
Operators
What operation is requested?
โ†’
3
Value
What does the expression produce?

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

Arithmetic a + b, a * b, a % b
Relational a > b, a == b
Logical a > 0 && b > 0
Assignment x = 10, x += 5
Conditional max = a > b ? a : b
Comma (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);
For a = 10 and b = 3:

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 thana < b
<=Less than or equala <= b
>Greater thana > b
>=Greater than or equala >= b
==Equal toa == b
!=Not equal toa != b
int a = 10;
int b = 20;

printf("%d", a < b);
1
Do not confuse: = means assignment, while == means comparison.

7.6 Logical Expressions

Logical operators combine or modify conditions.

Operator Meaning Example
&&Logical ANDage >= 18 && age <= 60
||Logical ORmarks < 40 || attendance < 75
!Logical NOT!(x == 0)
int age = 25;

int eligible = age >= 18 && age <= 60;

printf("%d", eligible);
1

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");
}
Because 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 10
b = 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 += 5x = x + 5
x -= 5x = x - 5
x *= 5x = x * 5
x /= 5x = x / 5
x %= 5x = 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;
Step 1
5 * 2
Multiplication has higher precedence.
โ†’
Step 2
10 + 10
Now addition remains.
โ†’
Result
20

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 ++ -- + - ! ~, castUnary
* / %Multiplicative
+ -Additive
<< >>Shift
< <= > >=Relational
== !=Equality
&, ^, |Bitwise
&&, ||Logical
?:Conditional
Low= += -= *= /= %= and related assignmentsAssignment
Very important: Precedence tells you grouping. It does not mean that every operand in a complex expression is evaluated strictly from left to right.

7.12 Parentheses โ€” Make the Intent Obvious

int a = 10 + 5 * 2;
int b = (10 + 5) * 2;
Expression Grouping Result
10 + 5 * 210 + (5 * 2)20
(10 + 5) * 2(10 + 5) * 230
Parentheses are not only for changing a result. They also make the programmer's intention easier for another person to read.

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
Do not say โ€œC always evaluates left to right.โ€ Some operators associate left-to-right for grouping, while the actual evaluation order of operands can be different.

7.14 Integer Division

int a = 5;
int b = 2;

int result = a / b;

printf("%d", result);
2

Since both operands are integers, integer division is performed. The result is an integer; the fractional part is not retained.

Remember: The type of the operands at the time of the operation matters more than the type of the variable receiving the result.

7.15 Floating-Point Division

int a = 5;
int b = 2;

double result = (double)a / b;

printf("%.2f", result);
2.50
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);
A
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");
}
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;
Condition
a > b
โ†’
False
Choose b
โ†’
Value
20
The conditional operator produces a value; it is not a replacement for every 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
A good beginner habit is to use increment/decrement in a simple, separate statement when the value is not obviously being used.

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 + b
Final expression value = 30
Do not confuse the comma operator with commas used simply to separate function arguments or declarations. The comma operator is an actual operator when used in a comma expression.

7.23 Expression Evaluation โ€” A Worked Example

int result = 10 + 20 / 5 * 2 - 3;
1
20 / 5
= 4
โ†’
2
4 * 2
= 8
โ†’
3
10 + 8 - 3
Same precedence โ†’ left association
โ†’
4
15
Final value

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?
Example:
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
Do not use expressions like this in normal C programs. The program has undefined behavior because the modifications of 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.
Integer division 5 / 2 gives 2 when both operands are integers.
Precedence assumptions Use parentheses when the intended grouping is not immediately obvious.
Evaluation-order assumptions Do not assume every operand is evaluated left-to-right.
Unsafe ++ / -- Avoid multiple unsequenced modifications of the same object.
Complex one-liners Break difficult calculations into meaningful intermediate steps.
CodeBhavya Rule: If you cannot explain an expression step by step, simplify it. Good C code is not code that looks clever; it is code whose behavior is easy to understand.

7.27 Quick Revision

๐Ÿ“Œ Expression โ†’ Combination of operands and operators that produces a value.

๐Ÿ“Œ 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.

1. What is the result of 10 + 5 * 2?
2. What is the result of 5 / 2 when both operands are int?
3. Which operator has higher precedence?
4. What is the result of (10 + 5) * 2?
5. What is the value of b after int a = 5; int b = a++; ?
6. What is the result of (double)5 / 2?
PRACTICE

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.

๐Ÿ“ˆ Expressions Practice Progress
Solved 0 / 10
Completed with Solution 0
Total Score 0 / 1000
Completion 0%
A problem counts as Solved when all test cases pass without opening the full solution. Problems completed after viewing the official solution are tracked separately.
1. Evaluate 10 + 5 * 2

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.

2. Evaluate (10 + 5) * 2

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.

3. Evaluate 20 / 5 * 2

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.

4. Average of Two Integers

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.

5. Percentage Using Explicit Type Casting

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.

6. Pre-Increment Expression

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.

7. Post-Increment Expression

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.

8. Largest Using a Conditional Expression

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.

9. Evaluate a Mixed Expression

Problem 9: Write a C program to evaluate 10 + 20 / 5 * 2 - 3.

Input: No input.

Output: Print the result.

10. Integer Division vs Floating-Point Division

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.

7.30 Key Takeaway

๐ŸŽฏ Before evaluating any C expression:

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.
INTERACTIVE LEARNING

๐ŸŽฌ Expressions โ€” Step-by-Step Evaluation

Follow how C groups and evaluates 10 + 20 / 5 * 2 - 3 using precedence and associativity.

PROGRAM TRACING

๐Ÿ”Ž Program Tracing โ€” Expressions

Trace arithmetic, logical, and conditional expressions using actual C variables and outputs.

INTERVIEW PREPARATION

๐ŸŽค Expressions โ€” Interview Questions

1. What is an expression in C?
2. What is the difference between precedence and associativity?
3. Does operator precedence determine the evaluation order of independent operands?
4. Why does 5 / 2 produce 2 in an integer expression?
5. What is the purpose of explicit type casting in an expression?
6. What is the result of a relational or logical expression in C?
7. Why should expressions such as i++ + ++i be avoided?
8. How can parentheses improve expressions?
PLACEMENT TIPS

๐Ÿ’ก Expressions โ€” Extra Tips

  • For output-prediction questions, identify parentheses, then precedence, then associativity.
  • Always check the data types before evaluating division. 5 / 2 and 5.0 / 2 do 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.
EXTRA PRACTICE

โœ๏ธ Expressions โ€” Extra Practice Questions

  1. Evaluate 8 + 12 / 3 * 2 - 1 step by step.
  2. Compare the results of 7 / 2 and (double)7 / 2.
  3. Explain the grouping of 20 - 5 - 3.
  4. Predict the values after int a = 4; int b = ++a + 2;.
  5. Rewrite a complicated mixed expression using parentheses so the intended grouping is obvious.
  6. Explain why precedence alone is not enough to justify the behavior of i++ + ++i.
โ† Previous Topic: Operators Next Topic: Decision Making โ†’