AT>> char s[40];
AT>> fgets (s, 40, stdin);
NH> That will STILL give a case of the nasties if the user enters 40
NH> characters. You must make room for the terminating null.
NH> char s[41];
NH> fgets(s,40,stdin);
fgets() already makes room for the terminating NUL. It reads at most *one
less than* the number of characters specified in its second argument from the
input stream given in its third, thus leaving room for a NUL to be appended
without overflowing the buffer.
¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)
|