C Mistakes – scanf



Common Mistakes : Scanf Errors


1 : Don’t forget to write “&” before variable name

#include<stdio.h>
void main()
{
int num;
//Incorrect Way : scanf("%d",num); 
scanf("%d",&num);
}
  • Syntactically it won’t show any error message
  • Logically It shows you “Garbage” or “Unexpected Value”.
  • “&” should be written before “Variable” name .

2 : Don’t write “&” before Pointer variable

#include<stdio.h>
void main()
{
int num;
int *ptr;
ptr = &num;
//Incorrect Way : scanf("%d",&ptr); 
scanf("%d",ptr);
}
  • Pointer variable Stores address of variable where it points .
  • “&” is used to access “Address of Variable” , therefor no need to write “&” before pointer variable .
  • Pointer stores the address of another variable , it accesses variable directly.