Understanding of Programming Errors in C Language
Errors are issues or faults that occur in a program, causing abnormal program behavior. Even experienced developers can make these mistakes.
Errors are issues or faults that occur in a program, causing abnormal program behavior. Even experienced developers can make these mistakes.
These errors can be detected either during compilation or execution. Therefore, it is essential to eliminate these errors for successful program execution.
For example:
Correct: int a; // This is the correct form
Incorrect: Int a; // This is an incorrect form
Common syntax errors include missing parentheses, attempting to use a variable without declaring it, and forgetting the semicolon at the end of a statement.
Let’s illustrate with an example:
#include <stdio.h>
int main()
{
a = 10; // Error: 'a' is undeclared
printf("The value of a is : %d", a);
return 0;
}
For example:
#include <stdio.h>
int main()
{
int a = 2;
int b = 2 / 0; // Error: Division by zero
printf("The value of b is : %d", b);
return 0;
}
For example:
#include <stdio.h>
int Main() // Error: Should be 'main()' not 'Main()'
{
int a = 78;
printf("The value of a is : %d", a);
return 0;
}
For example:
#include <stdio.h>
int main()
{
int sum = 0; // Variable initialization
int k = 1;
for (int i = 1; i <= 10; i++); // Logical error: Semicolon misplaced
{
sum = sum + k;
k++;
}
printf("The value of sum is %d", sum);
return 0;
}
In the above code, the semicolon (;) after the for loop leads to incorrect program output.
For example:
#include <stdio.h>
int main()
{
int a, b, c;
a = 2;
b = 3;
c = 1;
a + b = c; // Error: Invalid statement
return 0;
}
In the above code, the statement ‘a + b = c’ is incorrect because two operands cannot be used on the left side.
These are the common types of programming errors in C.
Subscribe to get the latest posts sent to your email.