{ }. The compiler treats the entire
block as a single statement.
Syntax
{
statement1;
statement2;
statement3;
}
The statements inside the braces are executed sequentially from top to bottom.
Example 1: Compound Statement
#include
int main()
{
{
printf("Welcome\n");
printf("to\n");
printf("C Programming");
}
return 0;
}
Output
Welcome
to
C Programming
Example 2: Compound Statement with if
#include
int main()
{
int marks = 75;
if (marks >= 40)
{
printf("Result: Pass\n");
printf("Congratulations!");
}
return 0;
}
Output
Result: Pass
Congratulations!
Both printf() statements belong to the if block because
they are enclosed within curly braces.
Example 3: Compound Statement Inside a for Loop
#include
int main()
{
int i;
for(i = 1; i <= 3; i++)
{
printf("Iteration %d\n", i);
printf("Hello World\n");
}
return 0;
}
Output
Iteration 1
Hello World
Iteration 2
Hello World
Iteration 3
Hello World
Structure of a Compound Statement
{
Statement 1;
Statement 2;
Statement 3;
}
Everything enclosed between the opening brace { and the closing
brace } forms a single compound statement.
Characteristics of Compound Statements
| Characteristic | Description |
|---|---|
| Enclosed by Braces | Always written inside { }. |
| Contains Multiple Statements | Can contain one or more executable statements. |
| Treated as One Statement | The compiler considers the entire block as a single statement. |
| Supports Variable Declarations | Local variables can be declared inside the block. |
| Creates a New Scope | Variables declared inside the block are accessible only within that block. |
Where Are Compound Statements Used?
| Construct | Purpose |
|---|---|
if |
Execute multiple statements when the condition is true. |
if...else |
Group multiple statements in both branches. |
for |
Repeat multiple statements in each iteration. |
while |
Execute a block repeatedly while the condition is true. |
do...while |
Execute multiple statements at least once. |
| Functions | The body of every function is a compound statement. |
main() function, is itself
a compound statement because it is enclosed within curly braces.
Single Statement vs Compound Statement
| Feature | Single Statement | Compound Statement |
|---|---|---|
| Number of Statements | One | One or More |
| Uses Curly Braces | Usually No | Yes |
| Acts as One Statement | Yes | Yes |
| Can Declare Local Variables | No | Yes |
Common Mistakes
Avoid These Mistakes
- Forgetting the opening or closing curly brace.
- Assuming multiple statements belong to an
ifstatement without braces. - Using incorrect indentation, making the code difficult to read.
- Declaring variables outside their intended scope.
Best Practices
- Always use curly braces, even for a single statement.
- Indent statements inside the block consistently.
- Keep related statements within the same compound statement.
- Declare local variables as close as possible to where they are used.
Prerequisites
Before Learning This Topic
- Basic understanding of C program structure.
- Knowledge of statements and semicolons.
- Familiarity with conditional statements and loops.
- Basic understanding of curly braces
{ }.
Interview Questions
- What is a compound statement in C?
- Why is a compound statement also called a block statement?
- Can a compound statement contain only one statement?
- Where are compound statements commonly used?
- Does a compound statement create a new scope for variables?
- Why are curly braces important in compound statements?
Key Takeaway
A compound statement is a block of one or more statements
enclosed within curly braces ({ }). It enables multiple
statements to be treated as a single unit, making it essential for control
structures, functions, and variable scoping in C programming. Compound
statements improve code organization, readability, and maintainability.