Difference between Expression and Statement in C

In C programming, expressions and statements are two fundamental concepts, but they serve different purposes.

What is an Expression in C?

An expression in C is any valid combination of variables, constants, operators, and function calls that produces a value.

Think of it like a math equation. You’re calculating something and expecting a result.

Example:

a + b

This is an expression. It adds the value of a and b and gives you a result. But on its own, it doesn’t do anything unless you use it in a statement.

x = 5    // Here, 'x = 5' is also an expression. It assigns 5 to x and returns 5.
x * y + 10  // Multiplies x and y, adds 10.

Expressions are used inside statements.

advertisement

What is a Statement in C?

A statement in C is a complete instruction that tells the computer to perform an action. It ends with a semicolon (;).

In simple words, a statement is like a command or task.

Example:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
x = a + b;

This is a statement. It tells the computer to add a and b, and store the result in x.

Types of Statements:

  • Expression statement – like x = 5;
  • Declaration statement – like int a;
  • Control statement – like if, while, for, etc.
  • Compound statement – a group of statements inside { }

Key Differences Between Expression and Statement in C

Here’s a comparison table highlighting the key differences between Expression and Statement in C.

Feature Expression Statement
Definition A combination of variables, constants, and operators that evaluates to a value. A complete instruction that performs an action.
Result Always returns a value. May or may not return a value.
Usage Can be part of a statement. Can contain expressions or be a control structure.
Assignment Can be assigned to a variable. Example: x = a + b; Cannot be directly assigned to a variable.
Examples a + b, x * y, 10 / 2 if, while, return, x = 5;
Purpose Used to compute values. Used to perform actions or control flow.
Syntax Requirement No semicolon needed when used within a statement. Usually ends with a semicolon (except blocks and control structures).
Independence Cannot stand alone as a line of code. Can stand alone as a complete instruction.
Function Calls Treated as an expression when used in another expression, e.g., y = pow(2, 3); Treated as a statement when used alone, e.g., pow(2, 3);

Simple Code Example in C

#include <stdio.h>
 
int main() {
    int a = 10, b = 20;
    int sum;
 
    sum = a + b;      // 'a + b' is an expression, 'sum = a + b;' is a statement
    printf("%d\n", sum); // This is another statement
 
    return 0;
}

This C program adds two numbers and prints the result. It starts by including the standard input-output library. Inside the main() function, two integers a and b are given values 10 and 20. Their sum is stored in the variable sum. The printf() function then displays the sum, which is 30. The program ends with return 0;, showing that it ran successfully.

Side Effects, Expression Statements, and Null Statements in C

1. Side Effects in Expressions:

Some expressions, like assignment (x = 5), can have side effects. An expression that changes the state of variables is a typical side effect. For example:

x = 5;  // Expression with a side effect (it changes the value of x)

2. Expression Statements:

advertisement

Expressions can be used as statements on their own. For example, a function call is an expression but can be used as a statement:

printf("Hello, World!");  // Expression statement

3. Null Statement:

The null statement (a semicolon ; alone) is a valid statement in C, but it does nothing. It’s typically used when a statement is syntactically required, but no action is needed:

for (int i = 0; i < 10; i++)
    ;  // Null statement, does nothing but is syntactically correct

4. Common Mistakes:

A typical mistake is confusing an expression with a statement, especially when semicolons are missing or incorrectly placed.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.