>>What if I really wanted to enter 1 followed by 500 zeros?
Try it and see, just like any good programmer
>>Do you think I could actually do it?
Der, yeah!
>>How should the program handle it?
fgets() will read as much as it can, and the rest will remain in the input buffer, waiting for the next read.
>>Is this a valid number?
Depends on your definition of valid. 21 isn't valid if you only accept 1 through 19.
>>Should all programs check validity of input
Wherever practicle, yes, do your best. For some programs, input validation is essential, for example a server that is public-facing. A buffer overflow is the source of many worms But then, a small program that is only going to be run locally (by yourself, say) might not require a bullet proof interface.
In this code:
... a lack of \n does not necessarily mean there's more data to come (ie full data load). This could simply be the last line in the file, that doesn't actually contain a \n character on it.Code:char buf[BUFSIZ] = {0}; fgets( buf, BUFSIZ, stdin ); if( strchr( buf, '\n' ) == NULL ) { ...more than BUFSIZ entered, deal with it... }
>>Is the following legal?
>>char buf[len];
Yes, in C99. No, not in C89.
There's more info on reading numbers here:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
and a short note on reading strings here
http://faq.cprogramming.com/cgi-bin/...&id=1043284385