i have a piece of code in C .But i am not able to understand its output
#include<stdio.h>
main()
{
char a1[20];
char a2[30];
char a3[40];
scanf("%s",&a1);
gets(a2);
fgets(a3,sizeof(a3),stdin);
printf("%d,%d,%d\n",strlen(a1),strlen(a2),strlen(a3));
}
When i enter my input like
amit
singh
output comes out to be 4,0,6 and fgets doest not allow me to enter any string ,i am able to enter only 2 inputs?
"amit\nsingh\n"scanfconsumes “amit” (and writes that intoa1)getsconsumes “\n” (and writes empty string toa2)fgetsconsumes “singh\n” (which it writes toa3)The output is correct.
Do not EVER use
gets!