Programming Errors in C
Common Programming Errors
When programming in C, various types of errors can occur. These errors can be broadly classified into the following categories:

- Syntax Errors: Errors that occur when the code does not conform to the syntax rules of C.
- Run-time Errors: Errors that occur during the execution of the program.
- Linker Errors: Errors that occur during the linking phase, typically related to undefined functions or variables.
- Logical Errors: Errors that produce incorrect output due to a flaw in the logic of the code.
- Semantic Errors: Errors that arise when the statements are not logically valid or understandable to the compiler.
Syntax Errors
Syntax errors occur when the code violates the grammatical structure of the C language. Common causes include missing semicolons, misplaced parentheses, or incorrect variable types.
Example of Syntax Error
#include <stdio.h>
int main()
{
int x = 10 // Missing semicolon at the end
printf("x = %d", x);
return 0;
}
In the above example, the semicolon is missing at the end of the declaration of the variable x
, causing a syntax error.
Run-time Errors
Run-time errors occur during the execution of the program. These errors often happen when the program is running, and it cannot perform the expected operation. A common example is division by zero.
Example of Run-time Error
#include <stdio.h>
int main()
{
int a = 2;
int b = 2 / 0; // Division by zero
printf("The value of b is : %d", b);
return 0;
}
Output
In this example, the program throws a run-time error due to division by zero, which is undefined in mathematics.
Linker Errors
Linker errors occur when the executable file is not created successfully, often due to incorrect function prototypes or missing functions in the code.
Example of Linker Error
#include <stdio.h>
int Main() // Incorrect function name, should be 'main()'
{
int a = 78;
printf("The value of a is : %d", a);
return 0;
}
Output
In this example, the function name is written as Main()
instead of the standard main()
, causing a linker error.
Logical Errors
Logical errors occur when the program runs successfully but produces incorrect output due to a mistake in the logic of the program.
Example of Logical Error
#include <stdio.h>
int main()
{
int sum = 0;
int k = 1;
for (int i = 1; i <= 10; i++); // Logical error: semicolon after the loop
{
sum = sum + k;
k++;
}
printf("The value of sum is %d", sum);
return 0;
}
Output
In the above code, the semicolon after the for
loop causes the loop body to be skipped, resulting in an incorrect output.
Semantic Errors
Semantic errors are issues where the program’s logic or statements don't make sense in the context of the program's requirements, often leading to unexpected behavior or incorrect results.
Example of Semantic Error
#include <stdio.h>
int main()
{
int a, b, c;
a = 2;
b = 3;
c = 1;
a + b = c; // Invalid assignment, semantic error
return 0;
}
Output
In this example, the expression a + b = c
is invalid because you cannot assign a value to an expression like a + b
.