Ex24 question (or possible change to book code)

In Ex24 we use fgets like this:

fgets(you.first_name, MAX_DATA - 1, stdin);

Reading the man description of fgets it mentions:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.

With that in mind I found no obvious change in behavior by issuing MAX_DATA without the - 1 like this:

fgets(you.first_name, MAX_DATA, stdin);

So for all intents n purposes this seems like valid code because fgets itself stops one less than size characters. So would the former code be prematurely ending the input scan one extra character early and the latter being more accurate? Or is there a valid purpose for this extra decrement here?

Of course I say this after testing the input overflows. The resulting code errors end the program the same exact way.

I’m pretty sure that’s good code, but what do I know?

1 Like

Wellllllllllllll, I live by the rule that I never trust what the man pages tell me. I’ve been burned by fgets() before so I always subtract 1 so I know it is really truly leaving a ‘\0’ on the end.