在 C 中检查溢出 long long

Checking overflow long long in C

我想检查来自 fgets 的变量在 C 语言中没有溢出 ling long int。 我试过这个:

long long x; 
fgets(...)
...
if(x <= LLONG_MAX && x >= LLONG_MIN)

但它不起作用,因为 long long int 会收到太大的数字;它 回到 LLONG_MIN + excess(我认为是)。

尝试从 char* 转换为 long long 时使用 strtoll。这些行取自 strtoll 文档 (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).