Once scanf("%d",&a);
takes numeric characters out of stdin
to form the int
for a
, it is done. It does not consume a '\n'
that may follow it.
gets(aa);
, consumes data until '\n'
.
So input like 1 2 3 x y Enter to the below will put 123
into a
and "xy"
into aa
.
scanf("%d",&a);
gets(aa);
Input like 4 5 6 Enter to the above will put 456
into a
and ""
into aa
.
Recommend using fgets()
and add error handling.
if (fgets(aa, sizeof aa, stdin) == NULL) Handle_EOF();
if (sscanf(aa, "%d", &a) != 1) Handle_Nonnumeric_Input();
if (fgets(aa, sizeof aa, stdin) == NULL) Handle_EOF();
aa[strcspn(aa, "\n")] = '\0'; // truncate potential \n