![]() |
| Common Errors In C |
In this post i will tell about common errors we makes in C Programming Language as a beginner as well as trained c programmer. We are all humans, so we will make mistakes even-though we know the correct syntax or correct logic. In this post it is a collection of simple common mistakes that we makes in C Programming Language.
Common Errors !
# 1 :
Variable Declaration :
We most of the time forget to declare variables before using it. Especially if it is the variable in for loop we will definitively forget.
We most of the time forget to declare variables before using it. Especially if it is the variable in for loop we will definitively forget.
//This program will generate error because we didn't declared d that we are reading using scanf
main()
{
scanf("%d",&d);
printf("D = %d",d);
}
// Error in the above program can be rectified by declaring the variable 'd'
main()
{
int d;
scanf("%d",&d);
printf("D = %d",d);
}
# 2 :
Break Statement In a Switch Case :
If you are a fresher in C Programming language, sometimes we will forget to put break statement in Switch case. But it is not a Syntax or Compile Error but without Break statement it will execute all the statement in Switch case after the input case entered.
//Switch case without break
main()
{
int a=1
switch(a)
{
case 1 :
printf("www.invatac.com \n");
case 2 :
printf("By Rahul Babu R \n");
case 3 :
printf("How are you ? \n");
}
}
Output :
www.invatac.com
By Rahul Babu R
How are you ?
// Corrected Switch Case with Break Statement
main()
{
int a=1;
switch(a)
{
case 1:
printf("www.invatac.com \n");
break;
case 2:
printf("By Rahul \n");
break;
case 3:
printf("How are you ? \n");
break;
}
}
Output :
www.invatac.com
# 3 :
To put & for a variable when we use scanf to read variable:
//Program where we forget to put & in scanf
main()
{
int x;
printf("Enter The value of x");
//scanf("%d",x);//here you have to put &x otherwise it will be a error
scanf("%d",&x);//corrected form
}
# 4 :
The usage of = and == :
if you are a fresher there will be confusion in using = and == , it looks same but in C Programming Language this two operators have different meaning.
= is used for assigning value to a variable.
and == is used for comparing any two variables. It is used for checking the equality of the two variables.
Eg : we can write a==b, here we are checking whether a is equal to b. Here the return value will be 0 or 1. If a is equal to b then the return value will be 1 and otherwise it will be zero
# 5 :
Function Declaration :
We normally forget to declare function, Both Freshers and Masters will forget this function declaration on the first attempt.
If we are using any User defined function other than main(). we should declare after the preprocessor derivatives.
//Function Declaration
#include<stdio.h>
void sum(int,int); //Function Declaration this declaration is compulso
main()
{
int a=10,b=9;
sum(a,b);
}
sum(int a,int b)
{
printf("Sum = %d\n",a+b);
}
There are so many common errors we will write in a C Program, i am working on this errors now and stay tune to invatac will update with new list of other common errors.
Hope you all have liked this post, if you have doubts In C Programming language please feel free to comment below.

No comments:
Post a Comment