Programming Essentials in C - Lecture 6
Difference Between Syntax and Semantics
| Aspect | Syntax | Semantics |
|---|---|---|
| Definition | The set of rules that define the structure and format of code. | The meaning and logic of the code. |
| Focus | How code should be written (grammar, symbols, structure). | What the code does (its behavior and output). |
| Errors | Syntax Errors occur when the rules of the language are broken. | Semantic Errors occur when the code runs but gives incorrect results. |
| Checking | Checked at compile-time (before execution). | Checked at runtime (while the program runs). |
| Example of Incorrect Usage | int 5num = 10; (Invalid variable name - syntax error). | int a = 5 / 0; (Dividing by zero - semantic error). |
Syntax Errors
- Missing Semicolon:
int x = 5
printf("%d\n", x);Error: The statement int x = 5 is missing a semicolon at the end. The C compiler expects statements to be terminated by a semicolon.
- Undeclared Variable:
a = 10;
printf("%d\n", a);Error: The variable a is used without being declared first. In C, you must declare a variable (specify its type) before you can use it.
- Mismatched Braces:
int main() {
printf("Hello\n");
if (5 > 2) {
printf("Condition is true\n");
}
// Missing closing brace for main functionError: The closing curly brace } for the main function is missing. Braces must be properly opened and closed to define code blocks.
Semantic Errors:
- Type Mismatch in Assignment:
int num = "hello";Error: You are trying to assign a string literal "hello" to an integer variable num. The types are incompatible, even though the statement is syntactically correct.
- Dividing by Zero:
int a = 10;
int b = 0;
int result = a / b;
printf("%d\n", result);Error: Dividing an integer by zero is a mathematically undefined operation. While the C code is syntactically valid, it leads to undefined behavior or a runtime error.
- Using
printfwith Incorrect Format Specifier:
int value = 123;
printf("%s\n", value);Error: The format specifier %s is used to print a string (a sequence of characters), but value is an integer. This mismatch in the expected data type can lead to incorrect output or a runtime error.