gpt4 book ai didi

c - printf ("%llx"有什么问题)?

转载 作者:太空狗 更新时间:2023-10-29 16:41:10 27 4
gpt4 key购买 nike

我有一段代码挑战我对 C 的所有知识。我这里有:

int main(void){
unsigned long long int massage ;

scanf("%llX", &massage); //input: 0x1234567890abcdef
printf("%llX", massage);
return 0;
}

在我的“64bit - Corei5 - Fedora - GCC”上,它打印出我输入的内容。但在我伙伴的系统(32 位、MS XP、MinGW)上,它打印出 90ABCDEF。我不明白为什么。有谁知道吗?

顺便说一句:sizeof(unsigned long long int) 在他的系统上是 8。

最佳答案

问题是编译器认为的(如 sizeof 所反射(reflect)的:sizeof(unsigned long long int) 在编译时求值)和运行时库相信(正如 printf 中所反射(reflect)的:printf 函数在运行时被调用,所以那是它的格式说明符生效的时候)。

根据 "C99" in the MinGW documentation :

GCC does not include a C runtime library. This is supplied by the platform. The MinGW port of GCC uses Microsoft's original (old) Visual C runtime, MSVCRT, which was targeted by Microsoft Visual Studio 6 (released in 1998).

[…]

Because MinGW relies on MSVCRT, it has many of the same limitations and quirks with compatibility as Visual Studio 6. You should assume that MinGW applications cannot rely on C99 behaviour, only on C89. For example, the newer format characters in printf like %a and %ll are not supported, although there exists a workaround for %ll.

(它提到的解决方法是使用 I64 而不是 ll:所以,%I64X。烦人的是,至少在我的系统上, GCC 在看到文字格式字符串时会发出警告,因为它假定它会有更好的运行时库。)

关于c - printf ("%llx"有什么问题)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14071713/

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