Common mistakes we do in C programming
Sometimes, when we write a program in C language, we make mistakes and after that we says " Ohh? Why do i get error". So here are some common mistakes, which are made by everyone.
1. Undeclared Variables:
See the following example:
See the following example:
See the following example:
See following example:
See the following example:
1. Undeclared Variables:
See the following example:
int main() { printf("Enter a number: "); scanf("%d",&a); return 0; }In this program, your compiler don't know what is a. So need to declare variables before use.
int main() { int a; printf("Enter a number: "); scanf("%d",&a); return 0; }2. Forget & sign:
See the following example:
#include<stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d",n); return 0; }When we get value of any variable from keyboard, we use scanf() function. And in scanf() function, we don't write & sign and get error.
#include<stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d",&n); return 0; }3. Forget Variable Increment:
See the following example:
#include<stdio.h> int main() { int n=0; while(n<=10) { printf("%d",n); } return 0; }We forget to increase variable and say why my loop is going infinite.
#include<stdio.h> int main() { int n=0; while(n<=10) { printf("%d",n); n++; } return 0; }4. Setting a variable to an uninitialized value:
#include<stdio.h> int main() { int m,n; int sum=m+n; printf("Enter two numbers: "); scanf("%d%d",&m,&n); printf("Sum = %d",sum); return 0; }Here we will get a garbage value because we didn't initialized m and n when we used these in expression. So the value of sum will be a random value. To use variables in any expression, we need to initialize.
#include<stdio.h> int main() { int m,n; int sum; printf("Enter two numbers: "); scanf("%d%d",&m,&n); sum=m+n; printf("Sum = %d",sum); return 0; }5. Using single single equal sign to check equality
See following example:
#include<stdio.h> int main() { int n=19; if(n=15) { printf("ON"); } else printf("OFF"); return 0; }If we use a single equal sign to check equality, program will assign the value to the variable. Here the value of n is 19. If we use one equal sign then n will be assigned to 15 and if loop will be executed. So to check equality, we need to use two equal sign. After using two equal sign, first in if loop, program will check that if n is equal to 15 then if loop will be executed otherwise else loop.
#include<stdio.h> int main() { int n=19; if(n==15) { printf("ON"); } else printf("OFF"); return 0; }6. Extra Semicolons:
See the following example:
#include<stdio.h> int main() { int i,n=10; for(i=1;i<=10;i++); { printf("%d ",i); } return 0; }Sometimes we use extra semicolon and say why my loop is not working correctly. In this example, we have used a extra semicolon after for loop so it will terminate for loop before printf() function and we will get i as 11.
#include<stdio.h> int main() { int i,n=10; for(i=1;i<=10;i++) { printf("%d ",i); } return 0; }