C programming validating user input

Posted by / 10-Apr-2015 11:53

C programming validating user input

EOF is also returned if a read error occurs, in which case the error indicator for the stream is set, and errno is set indicate the error.

Beware of input in which a prefix of the input is a number (e.g., "1234wxyz"). If it doesn't, you're better off using the approach others have suggested: read the input line into a character string and use strtol() or strtod() to convert the number.

A better option is to read the input as text using . #define BUFSIZ (DIGITS) 3 // Account for sign character, newline, and 0 terminator ... fgets(input, sizeof input, stdin)) else My advice would be to check the return value of scanf().

These two functions also return a pointer to the first invalid character, so you can tell if the entire input string was consumed by the conversion.

Some programming languages leave input and output support to the libraries developed for the languages.

Unfortunately, will happily convert and assign the "12" and leave the "w4" in the input stream, fouling up the next read.

It will return a 1, indicating a successful assignment.

c programming validating user input-21c programming validating user input-29c programming validating user input-60

One thought on “c programming validating user input”