10

In the simplest way possible, how can I check if an integer initialized from function scanf is a number?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
genesisxyz
  • 778
  • 3
  • 14
  • 29

2 Answers2

9

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\n");
    } else {
        printf("Not an integer.\n");
    }
    return 0;
}

But it is suggest that you use fgets and strtol instead.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Thanks man, it works but if I put this in a loop, doesn't work properly, if the input is a string at the begin, the loop is infinite, I can resolve? – genesisxyz Jan 07 '10 at 19:50
  • 1
    `sscanf` and `scanf` is a bad idea. Both produce undefined behavior on overflow. – AnT stands with Russia Jan 07 '10 at 20:03
  • Good point. strtol is better, but he did ask how to use scanf, so I still think we ought to tell him the answer to his question, even if we are advising him not to use this method. – Mark Byers Jan 07 '10 at 20:18
  • 1
    The "%d" conversion specifier will read and convert up to the first non-digit character in the string. For an input like "12w3", it will convert "12" to 12 and assign it to v, leaving the string "w3" in the input stream. Since 'w' is not a digit, future calls to `scanf("%d", &v)` will fail (return 0) and not remove the offending character from the stream. Use `fgets()` to read the input as text, then convert using `strtol()` (or `strtod()` if you need floats). – John Bode Jan 07 '10 at 22:40
  • Please do not link to C++ documentation for C questions. And especially, do not link to incorrect documentation (the signature for `scanf()` the documentation gives is incorrect). – fuz Feb 20 '16 at 12:42
6

Your question is weirdly worded. An initialized integer is always a number (aside from exotic cases of trap representations), which means that there's no need to check anything.

I would guess that you need to check whether the given string is a valid representation of a number. For that, you first need to define what the valid representation should look like. Do you allow sign? Is a redundant + allowed (as a sign)? What about 0x prefix for hexadecimals? And so on.

C language offers its own set of rules that define the language's idea of a valid string representation of an integer. If that's what you need, then in order to verify whether the given string satisfies these rules, the best way is to use string-to-integer conversion function like strtol (and other functions from strto... group) and then check for the error condition.

Beware of the answers that suggest writing your own function that would verify these rules. This just doesn't make any sense, since the standard function exists already. Also, strictly speaking, in real-life programming there's rarely a need to perform verification without the actual conversion. strto... will do both.

Also, stay away from functions from scanf group to perfrom string-to-integer conversion. These functions produce undefined behavior on overflow (i.e. if the input string representation is too long). In general case, the only proper way to do such a conversion in C is functions from strto... group.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765