In the book Practical C Programming, I find that the combination of fgets()
and sscanf()
is used to read input. However, it appears to me that the same objective can be met more easily using just the fscanf()
function:
From the book (the idea, not the example):
int main()
{
int age, weight;
printf("Enter age and weight: ");
char line[20];
fgets(line, sizeof(line), stdin);
sscanf(line, "%d %d", &age, &weight);
printf("\nYou entered: %d %d\n", age, weight);
return 0;
}
How I think it should be:
int main()
{
int age, weight;
printf("Enter age and weight: ");
fscanf(stdin, "%d %d", &age, &weight);
printf("\nYou entered: %d %d\n", age, weight);
return 0;
}
Or there is some hidden quirk I'm missing?