Advertisement

Programmer Coding

Errors in C Programming

What is Errors in C?

A bug is a problem or glitch that causes a program to behave unexpectedly. Errors are also called errors or glitches, and the process of removing them is called debugging.

This problem was detected during compilation or execution. Therefore, for the program to be successful, the error must be removed from the program.

Advertisement

There are mainly five types of errors exist in C programming

 

  1. Syntax error
  2. Run-time error
  3. Linker error
  4. Logical error
  5. Semantic error
  • Syntax error

Syntax errors are also called compilation errors because they occur during compilation or we can say that syntax errors are thrown by the compiler. These errors are usually caused by errors in the input or by not following the syntax of the programming language. These mistakes are often made by beginners because they do not know the language. These errors can be easily debugged or fixed.

 Example
  1. #include <stdio.h>
  2. int main() {
  3. printf(“Hello, World!” // Missing semicolon
  4. return 0;
  5. }

Commonly occurred syntax errors are

 

  • If we don’t overlook the parentheses (}) while writing code.
  • Specify the value of a variable without declaration.
  • If we omit the semicolon (;) at the end of the expression.

 

  • Run-time error

 

Sometimes errors, called runtime errors, occur during execution even after execution is complete. The main cause of runtime errors is the failure to perform tasks while the program is running. Dividing by zero is an example of an error function. These errors are difficult to find because the compiler does not show them.

Advertisement
Example
  1. #include <stdio.h>

 

  1. int main() {
  2. int arr[5] = {1, 2, 3, 4, 5};
  3. printf(“Value at index 10: %d\n”, arr[10]); // Accessing out-of-bounds memory
  4. return 0;
  5. }
  • Linker error

Linker errors usually occur when the program’s executable is not created. This can occur due to incorrect prototyping or using incorrect header information. For example, the main.c file contains the sub() function, whose declarations and definitions are made in other files (such as func.c). During compilation, the compiler finds the sub() function in the func.c file and thus creates two files, main.o and func.o. When executed, if the sub() definition is not found in the func.o file, a linker error is thrown. The most common linker error occurs when we use Main() instead of main().

Example
  1. #include <stdio.h>
  2. intMain()
  3. {
  4. int a=78;
  5. printf(“The value of a is : %d”, a);
  6. return 0;
  7. }
  • Logical error

Logic errors are errors that cause unintended output. These errors produce incorrect output, but they are not errors and are called errors. Such mistakes are often made by beginners. The outcome of this error mostly depends on the developer’s perspective. If the programmer’s logic is sound, the likelihood of these errors is reduced.

In the code above, we are trying to print the 10-digit number, but since we added a semicolon (;) after the loop and obtained the wrong multiplication, The inside of the loop message will not be executed. This causes incorrect output.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int sum = 0;
  4. for (int i = 1; i <= 10; i++) { // Incorrect loop condition
  5. sum += i;
  6. }
  7. printf(“Sum of numbers from 1 to 10: %d\n”, sum); // Incorrect result
  8. return 0;
  9. }
  • Semantic error

Semantic errors are errors that occur when the compiler cannot understand an expression.

Example
  1. #include <stdio.h>
  2. intmain()
  3. {
  4. inta,b,c;
  5. a=2;
  6. b=3;
  7. c=1;
  8. a+b=c; // semantic error
  9. return0;
  10. }
Semantic errors can occur in the following cases:
  • Use of a un-initialized variable.

int i;
i=i+2;

  • Type compatibility

int b = “javatpoint”;

  • Errors in expressions

int a, b, c;
a+b = c;

  • Array index out of bound

int a[10];
a[10] = 34;

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top