In the simplest way possible, how can I check if an integer initialized from function scanf is a number?
scanf
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
On success, [scanf] returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.
So you could do something like this:
#include <stdio.h> int main() { int v; if (scanf("%d", &v) == 1) { printf("OK "); } else { printf("Not an integer. "); } return 0; }
But it is suggest that you use fgets and strtol instead.
1.4m articles
1.4m replys
5 comments
57.0k users