Introduction
The terms expression and statement are closely related in C programming, but they are not the same. Beginners often confuse these concepts because expressions frequently appear inside statements.
Understanding the difference between expressions and statements is important because it helps you read, write, and debug C programs more effectively.
What Is an Expression?
An expression is a combination of operands and operators that evaluates to a single value.
Expressions can consist of constants, variables, function calls, operators, or a combination of these elements.
Syntax
operand operator operand
Examples of expressions:
Example
10 + 5
a * b
x > y
count++
sqrt(number)
Each of these expressions produces a value when evaluated.
What Is a Statement?
A statement is a complete instruction that tells the program to perform an action.
Statements may contain one or more expressions, but not every statement is an expression.
Syntax
statement;
Examples of statements:
Example
int age = 20;
age++;
printf("%d\n", age);
return 0;
Each line is a complete instruction executed by the program.
Key Difference
The main difference is simple:
- An expression produces a value.
- A statement performs an action.
An expression can exist inside a statement, but a statement does not always produce a value.
Expression Inside a Statement
Consider the following statement:
Example
result = a + b;
This line contains:
- The expression
a + b, which evaluates to a value. - The assignment expression
result = a + b, which also evaluates to the assigned value. - The entire line ending with a semicolon, which is an expression statement.
Comparison Table
| Expression | Statement |
|---|---|
| Produces a value. | Performs an action. |
| Can be part of a statement. | May contain one or more expressions. |
| May appear without a semicolon. | Usually ends with a semicolon or a block. |
| Can be used inside another expression. | Cannot normally be nested inside another statement as a value. |
| Evaluates to a result. | Controls program execution. |
Examples of Expressions
Arithmetic Expression
Example
x + y
This expression evaluates to the sum of x and y.
Logical Expression
Example
age >= 18
This expression evaluates to either true (non-zero) or false (zero).
Function Call Expression
Example
strlen(text)
The function returns the length of the string, making the function call itself an expression.
Examples of Statements
Declaration Statement
Example
int number = 10;
Selection Statement
Example
if (number > 0)
{
printf("Positive\n");
}
Loop Statement
Example
while (count > 0)
{
count--;
}
These examples perform actions rather than simply producing values.
Can an Expression Be a Statement?
Yes. In C, an expression followed by a semicolon becomes an expression statement.
Example
count++;
x = y + 5;
printf("Hello");
Each line is both:
- An expression that produces a value.
- A statement because it is terminated by a semicolon.
if, for, and while are statements that are not expressions.
Examples That Are Statements but Not Expressions
Example
if (x > 0)
{
printf("Positive\n");
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", i);
}
return 0;
These constructs control the program's execution. They do not evaluate to a value themselves.
Common Beginner Mistakes
- Thinking every statement produces a value.
- Confusing the expression
a + bwith the statementresult = a + b;. - Believing
iforwhileare expressions in C. - Forgetting that an expression statement usually ends with a semicolon.
Summary
Expressions and statements are fundamental concepts in C programming, and understanding their differences makes it easier to read and write correct programs.
| Expression | Statement |
|---|---|
| Produces a value. | Performs an action. |
| Can be used inside other expressions. | Executes one complete instruction. |
Examples: a + b, x > y, count++ |
Examples: if, while, return, int x = 5; |
In practice, expressions and statements work together constantly. An expression computes a value, while a statement uses that value to perform a task. Mastering both concepts is essential before learning operators, control flow, and functions in C.