{ }) can be used to enclose
even a single line of code in C. While braces are optional for a single
statement after control statements such as if, for,
while, and do-while, using them improves code
readability, maintainability, and helps prevent programming errors.
Syntax Without Curly Braces
If only one statement follows an if statement, curly braces are
optional.
if (marks >= 40)
printf("Pass");
Syntax With Curly Braces
The same program can be written using curly braces.
if (marks >= 40)
{
printf("Pass");
}
Both programs produce the same result.
Example 1: Single Statement Without Braces
#include
int main()
{
int number = 10;
if (number > 0)
printf("Positive Number");
return 0;
}
Output
Positive Number
Example 2: Single Statement With Braces
#include
int main()
{
int number = 10;
if (number > 0)
{
printf("Positive Number");
}
return 0;
}
Output
Positive Number
Example 3: Multiple Statements Require Braces
#include
int main()
{
int number = 10;
if (number > 0)
{
printf("Positive Number\n");
printf("Number is greater than zero.");
}
return 0;
}
Output
Positive Number
Number is greater than zero.
With Braces vs Without Braces
| Feature | Without { } |
With { } |
|---|---|---|
| Single Statement | Allowed | Allowed |
| Multiple Statements | Not Allowed | Required |
| Readability | Moderate | Better |
| Maintainability | Lower | Higher |
| Chance of Errors | Higher | Lower |
Common Programming Mistake
if (number > 0)
printf("Positive Number");
printf("Program Finished");
Many beginners think both printf() statements belong to the
if statement. However, only the first statement is controlled by
the if. The second statement executes regardless of the condition.
Correct Version:
if (number > 0)
{
printf("Positive Number");
printf("Program Finished");
}
Advantages of Using Curly Braces
Benefits
- Improves code readability.
- Prevents logical errors.
- Makes future modifications easier.
- Allows multiple statements inside a control block.
- Follows professional coding standards.
Common Mistakes
Avoid These Mistakes
- Assuming braces cannot be used for a single statement.
- Forgetting braces when adding additional statements later.
- Writing multiple statements without braces.
- Incorrect indentation that hides logical errors.
Best Practices
- Use curly braces even for a single statement.
- Maintain consistent indentation.
- Always use braces in professional projects.
- Keep related statements inside the same block.
Prerequisites
Before Learning This Topic
- Basic understanding of C program structure.
- Knowledge of conditional statements (
if). - Basic familiarity with loops such as
forandwhile.
Interview Questions
- Can curly braces be used for a single statement in C?
- Are curly braces mandatory for a single statement after an
ifstatement? - Why do professional programmers use braces even for a single statement?
- When are curly braces compulsory in C?
- What problems can occur if braces are omitted?
Key Takeaway
Yes, curly brackets ({ }) can enclose a single line of code in
C. Although they are optional for a single statement, using them is a
recommended coding practice because they improve readability, reduce the
possibility of logical errors, and make programs easier to maintain. When
multiple statements are present, curly braces are mandatory.