c - 在 c 中用整数修复 u 导致 atol 发生变化

标签 c

下面的代码给出了 2147483647 的输出。 如果if(atol(str)<=2147483647u ) 更改为 if(atol(str)<2147483647u) , 输出为 100。 输入与 str= "2147483649" 相同.

#include <stdio.h>
#include <ctype.h>

int main()
{
    unsigned long  l = 100;
    unsigned char str[19] = "2147483649";

    if(atol(str)<=2147483647u)
    {
        l = atol(str);
    }
    printf("\n%ld",l);

    return 0;
}

最佳答案

INT_MAX = 2147483647

atol() 返回一个 long

对于大于 int max 2147483647 的数字

改用atoll()

#include <stdlib.h>

       int atoi(const char *nptr);
       long atol(const char *nptr);
       long long atoll(const char *nptr);

关于c - 在 c 中用整数修复 u 导致 atol 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16771298/

相关文章:

c - 在 PIC 上找到正确的端口并实现蜂鸣器

c - 如何使用 C 显示数组中每个字母字符的出现次数?

c - 在 WM_MOUSEMOVE 中绘图

c - 在 Hp-UX ksh 中执行交互式命令(意外)并获取子进程

c - 在 Linux C 中交替同步两个进程

shell - 如何使用 C 程序的选项运行 'ls'?

c - 打印在另一个函数中填充的数组

c - 为什么这些构造使用增量前和增量后未定义的行为?

c - 幂函数 K&R

'setw' 函数的 C 等价物