Question: So I have an assignment to figure out whether a number on the command line is either an integer or a double.

I have it mostly figured it out by doing:

```
sscanf(argv[x], "%lf", &d)

```

Where "d" is a double. I then cast it to an int and then subtract "d" with itself to check to see if it is 0.0 as such.

```
d - (int)d == 0.0

```

My problem is if the command line arguments contains doubles that can be technically classified as ints.

I need to classify 3.0 as a double whereas my solution considers it an int. 
For example initializing the program.

```
a.out 3.0

```

I need it to print out

```
"3.0 is a double"

```

However right now it becomes

```
"3 is an int." 

```

What would be a way to check for this? I did look around for similar problems which led me to the current solution but just this one edge case I do not know how to account for.

Thank you.

Answer: 
To test if a string will covert to a `int` and/or `double` (completely, without integer overflow, without *undefined behavior*), call `strtol()/strtod()`. [@Tom Karzes](https://stackoverflow.com/questions/46250408/how-to-check-an-edge-case-in-taking-command-line-argument-in-c-and-evaluating-to/46252356#comment79464780_46250408)

The trouble with a `sscanf()` approach is that the result is *undefined behavior* (UB) on overflow. To properly detect, use `strtol()/strtod()`.

```
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>

bool is_int(const char *src) {
  char *endptr;
  // Clear, so it may be tested after strtol().
  errno = 0;
  // Using 0 here allows 0x1234, octal 0123 and decimal 1234.
  // or use 10 to allow only decimal text.
  long num = strtol(src, &endptr, 0 /* or 10 */);

#if LONG_MIN < INT_MIN || LONG_MAX > INT_MAX
  if (num < INT_MIN || num > INT_MAX) {
    errno = ERANGE;
  }
#endif

  return !errno && endptr > src && *endptr == '\0';
}

bool is_double(const char *src) {
  char *endptr;
  // Clear, so it may be tested after strtod().
  strtod(src, &endptr);

  // In this case, detecting over/underflow IMO is not a concern.
  return endptr > src && *endptr == '\0';
}

```
```
int main (int argc,char *argv[])
{
    if(argc==2)
    {
        int i;
        double d;
        d=atof(argv[1]);
        i=atoi(argv[1]);
        if(d!=i)
            printf("%s is a double.",argv[1]);
        else if(d==i)
            printf("%s is an int.",argv[1]);

    }
    else
        printf("Invalid input\n");
    return 0;
}

```

You must add `#include <stdlib.h>`