Question Details

No question body available.

Tags

c cs50 fgets off-by-one

Answers (1)

Accepted Answer Available
Accepted Answer
May 19, 2026 Score: 4 Rep: 393,222 Quality: High Completeness: 50%

According to the specification, none of the words in this dictionary are longer than 45.

You must account for the Line Feed, as well as the NUL added by fgets. If the largest word has 45 characters, your buffer must have space for 47.

The dictionary does indeed contains a 45-character word:

         1         2         3         4     4
1234567890123456789012345678901234567890123456
pneumonoultramicroscopicsilicovolcanoconiosis␊

Including the line feed, that line contains 46 characters.

fgets places the first 44 and a NUL in word in one pass, then the last character of the word, the LF and a NUL in the next pass. So that line increases the count by two.

Increase the buffer size to 47 (or use an approach that doesn't require knowing the maximum word length in advance).