Friday, August 16, 2019

Errors in C programming

Errors in C:

Errors are the mistakes which are created by the programmer. It is also known as bugs.It is abnormal condition whenever it occurs the execution of program is stopped.

Basically, there are five types of errors in c programming:
1. Runtime error
2. Compile error
3. Semantic error
4. Logical error
5. Linker error

Runtime Errors:

Those errors that occur during the execution of  a c program and generally occur due to some illegal operation performed in the program.
For example: Divide any number by zero, Lack of free memory space, etc.
These type of error are hard to find as the compiler doesn't point the line at which the error occurs.

// C program to illustrate run-time error
#include<stdio.h>
void main()
{
   int n=9,d;
   d=n/0;
   printf("d=%d",d);
}

Error:
 warning: division by zero
                 d=n/0;

Compile Errors:

Errors that are generated at the time of compilation is known as compile time error, in general these are raised while break down the rules and regulations of programming language.
For example: Missing semicolon, writing keywords in upper case, writing variable declaration,etc.
It is also known as syntax error.

//C program to illustrate compile error
#include<stdio.h>
void main()
{
    int a=10;
    printf("%d",a)     //semicolon missed
}

Error:
   error: expected ';' before '}' token.

Semantic Errors:

This error occurs when the statements written in the program are not meaningful to the compiler.
For example:
//C program to illustrate semantic error
#include<stdio.h>
void main()
{
    int a,b,c;
    a+b=c;               //semantic error
 }


Error:
    error: lvalue required as left operand of assignment
    a+b=c;   //semantic error

Logical Errors:

The type of errors which provide incorrect output but appears to be error free are called logical errors.It occur when a program does not do what the programmer experts it to do.
For example:
// C program to illustrate logical error
#include<stdio.h>
void main()
{
      int i;
      int f=1;
      for(i=1;i<=5;++i)
          f=f+i;              //Logical error. Correct statement: f=f*i;
}

Error:
The output which we were expecting will not be shown.

Linker Errors:

Errors that occurs when we do not include header files for the predefined functions used in a program and when we misspell a standard c function. 
For example:
// C program to illustrate linker error
#include<stdio.h>
int Main()                //Linker error as 'main' is misspell as 'Main'
{
    printf("Hello World!");
    return(0);
}


No comments:

Post a Comment

Write a program to find gcd of two numbers using recursion.

About:  The process in which a function calls itself is called recursion and the corresponding function is called as recursive function. ...