1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
long i;
long long j;
int ret = EXIT_SUCCESS;
char *p;
if(argc != 2)
{
fprintf(stderr, "Usage: %s <integer>\n", argv[0]);
ret = EXIT_FAILURE;
}
else
{
i = strtol(argv[1], &p, 10);
if(*p != '\0')
{
fprintf(stderr, "Could not convert %s to integer.\n", argv[1]);
ret = EXIT_FAILURE;
}
else if(errno == ERANGE)
{
errno = 0;
/*
* value overflows an long integer.
* trying long long integer (C99)
*/
j = strtoll(argv[1], &p, 10);
if(errno == ERANGE)
{
fprintf(stderr, "Conversion to integer leads to overflow.\n");
ret = EXIT_FAILURE;
}
else
{
printf("Value is %lld (long long integer).\n", j);
}
}
else
{
printf("Value is %ld (long integer).\n", i);
}
}
return ret;
} |
Partager