scanf() and gets() in C Language
To get input we can use scanf() or gets() functions.
gets() is used only for String data type.
scanf() can be used for all data types with type specifiers like %d %c %f etc
One thing you have to note before using gets() is fflush(stdin).
fflush(stdin) function will flush the input
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
char nm[12];
clrscr();
printf("\nEnter No :");
scanf("%d",&x);
printf("\nEnter Name :");
scanf("%s",&nm); //& is optional for string
//another method for getting string
fflush(stdin); //flush previous input
printf("\nEnter Name :");
gets(nm);
printf("\nName :%s",nm);
getch();
}
If we don’t use fflush(stdin) then input for gets(nm) will be ‘enter’ that is pressed after scanf().