Common Causes of Syntax Errors
| Cause | Example |
|---|---|
| Missing Semicolon | printf("Hello") |
| Missing Parentheses | if x > 10 |
| Missing Curly Braces | Unmatched { } |
| Misspelled Keywords | pritnf() instead of printf() |
| Missing Double Quotes | printf(Hello); |
| Invalid Variable Declaration | int 10num; |
Example 1: Missing Semicolon
#include
int main()
{
printf("Hello World")
return 0;
}
Compiler Error
error: expected ';' before 'return'
The semicolon (;) after the printf() statement is missing.
Correct Program
#include
int main()
{
printf("Hello World");
return 0;
}
Output
Hello World
Example 2: Misspelled Function Name
#include
int main()
{
pritnf("Welcome");
return 0;
}
Compiler Error
error: implicit declaration of function 'pritnf'
The function name printf() is misspelled as
pritnf().
Example 3: Missing Parentheses
#include
int main()
{
int age = 18;
if age >= 18
{
printf("Eligible");
}
return 0;
}
Compiler Error
error: expected '(' before 'age'
Every if condition must be enclosed within parentheses.
When Does a Syntax Error Occur?
Write Program
│
▼
Compile Program
│
▼
Compiler Checks Syntax
│
├── Correct Syntax → Executable File
│
└── Incorrect Syntax → Syntax Error
Syntax Error vs Logical Error vs Runtime Error
| Feature | Syntax Error | Logical Error | Runtime Error |
|---|---|---|---|
| Occurs During | Compilation | Execution | Execution |
| Detected By | Compiler | Programmer/Test Cases | Operating System/Runtime |
| Program Compiles? | No | Yes | Yes |
| Example | Missing semicolon | Wrong formula | Division by zero |
How to Avoid Syntax Errors
Best Practices
- Always end statements with a semicolon.
- Match every opening bracket with a closing bracket.
- Spell keywords and function names correctly.
- Use proper indentation to improve readability.
- Read compiler error messages carefully.
- Compile the program frequently while coding.
Common Mistakes
Avoid These Mistakes
- Forgetting semicolons (
;). - Missing or mismatched parentheses.
- Missing opening or closing curly braces.
- Misspelling keywords or function names.
- Leaving quotation marks incomplete.
- Declaring variables using invalid identifiers.
Prerequisites
Before Learning This Topic
- Basic understanding of C program structure.
- Knowledge of variables and data types.
- Familiarity with statements and functions.
- Basic understanding of the compilation process.
Interview Questions
- What is a syntax error in C?
- When are syntax errors detected?
- Who detects syntax errors—the compiler or the operating system?
- Can a program containing syntax errors be executed?
- What is the difference between syntax errors and logical errors?
- Give three examples of syntax errors in C.
Key Takeaway
A syntax error occurs when a C program violates the language's grammatical rules. These errors are detected by the compiler during compilation, preventing the program from executing until they are corrected. Common syntax errors include missing semicolons, mismatched brackets, misspelled keywords, and incorrect statement syntax.