Discover the types of errors in C language with example8 min read

Having knowledge of different types of errors in C; will help in debugging the code. Errors are human-made. No one in this entire world is perfect enough to do work without an error.

If you are getting errors in the code then CONGRATULATION you are going to be a great programmer.

Error means there is a solution. Always solve your errors never leave it like that. If you are unable to solve error just post your issue in our Facebook group.

Do you know most of the time we are getting the line numbers of error in code, This tutorial will helpful in solving errors, and understand different errors with suitable examples.

Problems are not stop signs, they are guidelines. – Robert Schuller

इस ट्यूटोरियल को हिंदी में पढ़ने के लिए, यहाँ क्लिक करें!

Types of Errors in c

Mainly we are having three types of errors.

  1. Compile-time error.
  2. Logical error.
  3. Run-time error.

We will study each errors in detail.

Different types of errors in C language

Compile-time error

The error occurs in the Preprocessor or Translator or Assembler or Linker stage is known as a compile-time error.

declaration errors are translator error

Compile time errors are further sub classified as.

  1. Syntax error
  2. Semantic error
  3. Linker error

– Syntax error

In any language there are certain rules and regulations to follow, In general, like our verbal language is having grammar, words, and many more things to keep in mind when we are using it.

In the same way, C programming is also having certain rules. If we violate the rules of writing C syntax then the compiler will throw an error.

This compiler errors indicates something which we need to be fixed before the code can be compiled.

All these errors are detected by the compiler so it is known as compile-time errors.

Most frequent errors we are getting are :

  • Missing parenthesis “}”
  • instead of “;” we used “:”
  • printing the variable value without declaring it.
  • forget to place “;” at the end
  • forget semicolon in the printf statement.

Eg.

#include<stdio.h>

int main(void)
{
     printf("Hello, Friends chai pilo \n") \\here we forgot to place semicolon
     
     return 0;
}
code with error to check compiler

This is our source code, we compiled it.

this is how compiler is showing result on terminal about compiler error

Compiler has thrown an error with a line number. “error: expected ‘ ; ‘ after expression”.

After solving compilation error code executed successfully

Solved error by placing “;” at the end of a printf statement. Recompiled the code and got the desired output.

– Semantic error

Semantic error occurs when the statements written in the program are not meaningful to the compiler.

#include <stdio.h>

int main(void)
{
     int a = 10, b = 20, c;
     a + b = c; //This is Semantic error
     return 0;
}
Code is having Semantic error

Above code is having Semantic error in 6th line. “a+b=c;”.

in compilation of code Semantic error

After compiling above code, compiler thrown an error in 6th line “expression is not assignable”.

solved Semantic error in the code

Solved the error “a+b=c;” by “c=a+b;” .

succesful compilation after solving Semantic error in code

After solving error code has successfully compiled.

– Linker error

In linker stage error tells that your code compiles fine, but some functions or library files are not found.

In other words, error generated due to wrong function prototyping or incorrect header files

Eg. If you typed print() instead of printf(), It will compile but in linking stage, it shows linking error (print function not found)

One of the most common linker error is writing Main() instead of main().

Code is having linker error by mistake written prinf instead of printf

In above code code developer by mistake typed prinf instead of printf in line number 5. Now lets compile and see how will compiler reacts.

compiler thrown linker error along with one warning

just ignore warning and focus on error it is looks like as shown below.

Undefined symbols for architecture x86_64:
  "_prinf", referenced from:
      _main in prog-0b9963.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Do you observed something different?

There is no error line number, means linker has not given where is the error.

Do you want to know? If yes then wait. I’ll explain at the end.


Logical error

On compilation and execution of a program, desired output is not obtained when certain input values are given.

Our code will look error-free but provides incorrect output are called logical errors.

This is the most common errors done by beginners of programming.

These errors solely depend upon logical thinking of the programmer, and very easy to solve this issue by following the step by step execution of code or by using a debugger.


Runtime error

The errors occur during running executable file after successful compilation is known as a run-time error.

It is also called a bug in code.

One of the most common run time error is division by zero. It is also known as “Division error”.

These types of errors are very difficult to find as a compiler is not pointing to any line at which error occurs.

this code is containing runtime error

Line number 7 is having a division error.

while compiling because of smart compiler it shown a warning

In compilation, compiler thrown an warning. If your compiler is smart enough then it will throw a warning.

when executed runtime error code it thrown an error

in a runtime, code thrown an error called “floating point exception ./a.out”


IMP Question

Q) Why linker is not giving line number?

Ans: As we already studied compilation stages in detail, in the previous article. The output of the assembler is an object file that we are giving to the linker as an input.

That object file is nothing but a binary file. A binary file is not containing any line number so that’s why the linker unable to give line number in error.


Summary

More error you solve, the more knowledge you will gain.

If you are not getting any error, You’re in the wrong path of learning. This means you just copy-pasting so that way you are not getting any error.

Ask this Questions when you encounter an error.

  • Does the compiler has thrown any line number? if YES then read the statement very carefully. if NO then error must be generated by the linker so check all function names are correct, desired library files are attached or not.
  • Ask Why? it has occurred.
  • What should I do now? What should I change?
  • try try try until you get the desired result. Once you quit it will become your habit so never quit.

A problem is a chance for you to do your best. – Duke Ellington


Also, Read

Compilation (part 1) and functions in C.

Compilation (part 2) and GCC command.

If you want to do practice and not did C programming environment setup you can use online GDB

Leave a Comment