×

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

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 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 initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.