Also, fscanf makes it harder to avoid buffer overflows. With fgets, it's easy:
Code:
#define NAME_LEN 25
char name[NAME_LEN];
fgets(name, sizeof(name), stdin); // whatever I change NAME_LEN to, the sizeof parameter ensures it wont overflow
With scanf, you can specify a maximum size of the char array you're reading into, but it needs to be hard-coded in the string:
Code:
#define NAME_LEN 25
char name[NAME_LEN];
scanf("%25[^\n]", name); // now, if I change NAME_LEN, I have to remember to change the scanf separately, or risk overflow
There is no easy way around it. You can get around that with some macro string quoting tricks or by sprintf'ing your format string into another string first.
That, along with some of the other reasons mentioned, are why I prefer to use fgets to read a line of input, and strtol/strtod family of functions, or sscanf (the extra 's' is for scanning a string), to parse out any non-string data. strtol/strtod are much more powerful and provide better error handling than sscanf for reading in numbers.
Originally Posted by
grumpy
...used an asterix...
Been reading many French comics lately, grumpy? (emphasis mine)