C programming errors.

Here we are going to learn some of the C programming language errors, their causes and solutions.

  • Making errors in C programming is common for the most elementary step towards developing your grip for the subject. 
  • You must know the proper debugging tips and tricks to identify the error. As a beginner in C you might have encountered many of them and with the basic knowledge you can obviously find the errors and finish the compilation of your program successfully. 
  • Here we will mention some common errors that beginners in C language commit and ways to tackle them.

Compile time error

  • The errors occurring during the compilation of a program leading to failure in the compilation are termed as compile time error. 
  • They are generally caused when the programmer does not follow the programming rules.

Run time error

  • These errors generally occur due to the incorrect logic used while programming. They are difficult to detect as compared to compile time errors. 
  • The programmer must check the code from the beginning in order to check where the logic is wrong.

Warnings

  • The warnings are generated by the compiler when the compiler detects some abnormality in the coding way or pattern. 
  • The program might occasionally crash or misbehave if warnings are neglected but that rarely occurs. 
  • In C warnings can be neglected but compiler errors and run time errors should be corrected to obtain the desired output.

1. Undeclared Variables

  • The variable x used, is undeclared in the function. Hence compiler is not aware of any such variable.

int main()

{
if(x>5)
printf("x is greater than 5\n");
}
  • The variable x used, is undeclared in the function. Hence compiler is not aware of any such variable.

2. Uninitialized variables


int lc;

while(lc<100)
{
lc++;
}
  • The loop counter (lc) used with the while loop is not initialized. 
  • It is compulsory to initialize the loop counter before using while loop. 
  • Hence the compiler will not enter the loop.

3. Using assignment operator (=) to check equality


char x='Y';


while(x='Y')
{
printf("x is equal to y\n");
}
  • Here, in the while loop condition x is assigned to y with a single equal to sign which is wrong. In order to check equality == sign should be used.

4. Incorrect use of semicolon


int main()

{
int a = 0 //Semicolon Undefined
while(i < 5);
{
printf("Hello World");
i++;
}
return 0;
}

  • In the above cod snippet, the while loop has been terminated with a semicolon which is undesirable. Loop statements are never terminated with a semicolon. 
  • Therefore the loop body doesn't get executed.

5. Missing operator in scanf function


#include<stdio.h>


int main()
{
int num1, num2;
printf("\nEnter the first number:\t");
scanf("%d", num1); 
return 0;
}
  • In the above case, & ampersand operator is not used in the scanf function. The compiler will successfully compile the program with no errors. 
  • But during run time the user will not be able to give the input to the program. This is a very common mistake that beginners make.

Post a Comment

Previous Post Next Post