×

Errors in C

Errors in C

Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the experienced programmer makes one. Programming errors are also referred to as bugs or faults, and removing these errors is termed debugging.

It is an illegal operation that a developer performs, which leads to the abnormal working conditions of that program. Errors remain unnoticed until and unless the compiler compiles or is executed by the runtime. Many errors exhibit the program from getting compiled and executed. Hence, they have to debug for smooth compiling and executing.

Errors lead to the abrupt and illegal operation of the logic of the program. Many errors in the C programming language are hidden and prevented from the program from compilation or execution. Hence, while executing any application, for that matter, one should successfully take out the errors present within the program.

Consider an instance where a program has been written to display the product of two numbers. But while defining the variables, a programmer forgets to put the semicolon at the end, which results in an error while compiling. Hence, they are either identified during compilation or execution.

Types of Error in C

There are five types of errors that exist in the C standard:

Syntax error

  1. These types of errors when the program or an application violates the rule of the programming standard.
  2. Syntax errors also known as the compilation errors as they occur at the compilation time or the syntax errors since they are thrown by the compilers.
  3. These are the errors that mainly occur due to the mistakes while typing down the code or not following the syntax according to the C standard.
  4. They are usually done by the beginners as they may be new to the programming language.
  5. They can be debugged easily and rewritten.

E.g.:

 int x;
 Int x; 

The above-mentioned syntax is different from each other and the latter gives the syntax error due to case sensitivity.

Common errors in these types include;

  • Missing semicolon at the end of the code.
  • Displaying value without any declaration.
  • Missing parentheses.
  • Case sensitivity.
 #include <stdio.h>
 #include <conio.h>
 int main()
 {
 x = 10;
 printf (“The value of a is: %d”, a);
 return 0;
 } 

Output:

Errors in C

Runtime error

  • Errors that occur at the time of execution of a program or a runtime after successful compilation are referred to as runtime errors.
  • One of the most common runtime errors occur when a number is divided by zero also known as division error.
  • These are the types of errors which are pretty hard to guess as the compiler does not point where the actual error occurs.

E.g.:

 #include<stdio.h>
 void main()
 {
 int n = 9, div = 0;
 div = n/0;
 printf("resut = %d", div);
 } 

Output:

Errors in C

Linker error

  • This type of errors is occurred when the program is compiled successfully, and trying to link the different objects file with the main object file.
  • Whenever this error is occurred, the executable is not generated such as wrong function prototyping, incorrect header files, and so on.
  • Whenever the main() function has case sensitivity issues, this error arouses.

E.g.:

 #include<stdio.h>
 main()
 {
 int x = 52;
 int y = 0;
 printf("Div : %f", x/y);
 } 

Output:

Errors in C

Logical error

  • A logical error is one of the types of error in the C language that arouses an undesired output.
  • They are error free but still give errors which are logical.
  • They occur when the errors depend upon the logical thinking of the developer.
  • If the programmers sound logically good, then there might be a fewer cases of these errors.

E.g.:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 int sum = 0;
 int k = 1;
 for (int i = 1; i <= 10; i++);
 {
 sum = sum +k;
 k++;
 }
 printf (“The value of the sum is %d”, sum);
 return 0;
 } 

Output:

Errors in C

In the above code snippet, a for loop has a semicolon hence the inner loop will not execute and produces the output as 1.

Semantic error

  • It is the type of error that occurs when it syntactically is correct but have no meaning.
  • If an expression is given at the left side of the assignment operator, then it generates a semantic error.

E.g.:

 #include<stdio.h>
 int main()
 {
 int a, b, c;
 a = 10;
 b = 20;
 a + b = c;
 } 

The following can be the cases for the semantic error:

  • Use of a un-initialized variable.
    int i;
    i = i+2;
  • Type compatibility
    int b = "tutorials and examples";
  • Errors in expressions
    int a, b, c;
    a+b = c;
  • Array index out of bound
    int a[10];
    a[10] = 34;

Related Topics

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.