Understanding Programming Errors in C Language

Errors are problems or faults that occur in a program, causing abnormal behavior, and even experienced developers can encounter these errors. Programming errors are also referred to as bugs or faults, and the process of eliminating these errors is called debugging. These errors are detected during compilation or execution. Therefore, errors must be removed from the program for it to execute successfully.
There are five main types of errors in C language:
  • Syntax Errors
  • Runtime Errors
  • Linker Errors
  • Logic Errors
  • Semantic Errors
Syntax Errors
Syntax errors are also known as compilation errors because they occur during compilation, or we can say that syntax errors are errors thrown by the compiler. These errors are mainly caused by input mistakes or failure to follow the specified syntax of the programming language. These errors are usually only made by beginners, as they are not yet familiar with the language. These errors can be easily debugged or corrected.

👇 Click to receive 👇

👉 C Language Knowledge Resources Collection
For example:
If we want to declare the variable of type integer,  int a; // this is the correct form  Int a; // this is an incorrect form.
#include <stdio.h>
int main()  {
    a = 10;
    printf("The value of a is : %d", a);
    return 0;
}
In the output above, we observe that the code throws an ‘a’ undeclared error. This error is simply a syntax error.
Another possible scenario for syntax errors is if we make a mistake in the basic structure. Let’s understand this situation through an example.
#include <stdio.h>
int main()  {
   int a=2;
   if(.)  // syntax error
    printf("a is greater than 1");
    return 0;
}
In the code above, we placed (.) in the “if” instead of a condition, which will generate a syntax error, as shown in the figure below.
Runtime Errors
Sometimes, even after successful compilation, errors may occur during execution, which are called runtime errors. A runtime error occurs when the program cannot perform an operation while running. Dividing by zero is a common example of a runtime error. These errors are difficult to find because the compiler does not point them out.
Let’s understand this through an example.
#include <stdio.h>
int main(){
   int a=2;
   int b=2/0;
   printf("The value of b is : %d", b);
   return 0;
}
In the output above, we observe that the code shows a runtime error, which is division by zero.
Linker Errors
Linker errors are mainly generated when the executable file of the program is not created. This can be due to incorrect function prototypes or using incorrect header files. For example, the main.c file includes the sub() function, while the declaration and definition of that function are done in another file (e.g., func.c). During compilation, the compiler finds the sub() function in the func.c file, generating two object files, main.o and func.o. At runtime, if the definition of the sub() function is not found in the func.o file, a linker error will be thrown. The most common linker error is using Main() instead of main().
Let’s understand this with a simple example.
#include <stdio.h>
int Main(){
   int a=78;
   printf("The value of a is : %d", a);
   return 0;
}
In the output above, we observe that the code shows a linker error, indicating that the main() function cannot be found.
Logic Errors Logic errors are errors that lead to incorrect output. These errors produce incorrect output, but they themselves are error-free, known as logic errors. These errors are mainly made by beginners. The occurrence of these errors largely depends on the developer’s logical thinking. If a programmer has good logical thinking, the chances of these errors occurring are lower.
Let’s understand this through an example.
#include <stdio.h>
int main(){
  int sum=0; // Variable initialization
  int k=1;
  for(int i=1;i<=10;i++); // Logic error because we placed a semicolon after the loop
  {
      sum=sum+k;
      k++;
  }
  printf("The value of sum is %d", sum);
  return 0;
}
In the code above, we attempted to print the sum of 10 numbers, but due to placing a semicolon (;) after the for loop, the internal statements of the for loop do not execute. This leads to incorrect output.
Semantic Errors Semantic errors occur when statements are not understandable to the compiler.
Here are some scenarios of semantic errors:
Using uninitialized variables.
int i;i=i+2;
Type incompatibility
int b = "javatiku";
Expression errors
int a, b, c;a+b = c;
Array out of bounds
int a[10];a[10] = 34;
Let’s understand this through an example.
#include <stdio.h>
int main(){
   int a, b, c;
   a = 2;
   b = 3;
   c = 1;
   a + b = c; // Semantic error
   return 0;
}
In the code above, we used a+b = c, which is incorrect because we cannot use two operands on the left side.
Understanding Programming Errors in C LanguageProgrammer Technical Exchange GroupUnderstanding Programming Errors in C Language
Scan the code to join the group, remember to note: city, nickname, and technical direction.

Leave a Comment