I want to check variable from fgets
isn't overflowing ling long int
in C language.
I tried this:
long long x;
fgets(...)
...
if(x <= LLONG_MAX && x >= LLONG_MIN)
but it is not working because long long int
will receive too large number; it
goes back to LLONG_MIN
+ excess (I think so).
Use strtoll
when you are trying to convert from char* to long long. These lines are take from strtoll documentation (strtoll) :
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).