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("
You entered: %d %d
", 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("
You entered: %d %d
", age, weight);
return 0;
}
Or there is some hidden quirk I'm missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…