gpt4 book ai didi

c - gcc long long int 宽度与 int 相同

转载 作者:行者123 更新时间:2023-11-30 18:33:04 25 4
gpt4 key购买 nike

我正在测试 gcc 版本 8.2

gcc 似乎将 long long int 视为 int 。有没有什么办法解决这一问题?

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULLONG_MAX : %lu\n", (unsigned long long int) ULLONG_MAX);
printf("Big Integer : %lu\n", (unsigned long long int) 1234567890123456);

printf("%d\n", sizeof(long long int));
return 0;
}

输出:

INT_MAX     :   2147483647
INT_MIN : -2147483648
LONG_MAX : 2147483647
LONG_MIN : -2147483648
UINT_MAX : 4294967295
ULLONG_MAX : 4294967295
Big Integer : 1015724736
8

C:\prog_c\c_pro>gcc --版本海湾合作委员会(MinGW.org GCC-8.2.0-5)8.2.0版权所有 (C) 2018 自由软件基金会...

最佳答案

printf 需要知道 longlong long 之间的区别,这就是 %lld (有符号)或 %llu (无符号):

    printf("ULLONG_MAX  :   %llu\n", (unsigned long long int) ULLONG_MAX);
printf("Big Integer : %llu\n", (unsigned long long int) 1234567890123456);

这应该按您的预期工作。否则, printf 仅从堆栈中抓取部分位,从而给出您得到的错误答案。

此外,如果您打开编译器警告,它会告诉您:

long.c: In function ‘main’:
long.c:13:5: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘long long unsigned int’ [-Wformat=]
printf("ULLONG_MAX : %lu\n", (unsigned long long int) ULLONG_MAX);
^
long.c:14:5: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘long long unsigned int’ [-Wformat=]
printf("Big Integer : %lu\n", (unsigned long long int) 1234567890123456);
^
long.c:16:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]

始终打开警告是一个非常非常好的主意 - 对于 GCC,我通常使用 -W -Wall - 所以它会告诉我出了什么问题。

编译器警告与 SO 类似,但没有反对票:-)

关于c - gcc long long int 宽度与 int 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59399883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com