C语言 带有GCC的C中的Printf long long int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13590735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:35:38  来源:igfitidea点击:

Printf long long int in C with GCC?

cgccprintfc99

提问by user963241

How do I printf long long intand also unsigned long long intin C99 using GCC?

我如何使用 GCC 在 C99 中打印long long intunsigned long long int打印?

I have searched the other posts which suggest to use %lldbut it gives these warnings:

我已经搜索了其他建议使用的帖子,%lld但它给出了以下警告:

warning#1: unknown conversion type character 'l' in format [-Wformat]|
warning#2: too many arguments for format [-Wformat-extra-args]|

警告#1:格式[-Wformat]| 中的未知转换类型字符“l”
警告#2:格式参数过多 [-Wformat-extra-args]|

For the following attempt:

对于以下尝试:

#include <stdio.h>

int main()
{
   long long int x = 0;
   unsigned long long int y = 0;
   printf("%lld\n", x);
   printf("%llu\n", y);
}

回答by nos

If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64dfor a 64 bit integer. (and %I64ufor an unsinged 64 bit integer)

如果您在 Windows 上使用 mingw,gcc 使用 win32 运行时,其中 printf 需要%I64d一个 64 位整数。(%I64u对于未标记的 64 位整数)

For most other platforms you'd use %lldfor printing a long long. (and %lluif it's unsigned). This is standarized in C99.

对于大多数其他平台,您将%lld用于打印 long long。(%llu如果它是未签名的)。这是在 C99 中标准化的。

gcc doesn't come with a full C runtime, it defers to the platform it's running on - so the general case is that you need to consult the documentation for your particular platform - independent of gcc.

gcc 没有完整的 C 运行时,它依赖于它运行的平台 - 所以一般情况是你需要查阅特定平台的文档 - 独立于 gcc。

回答by effeffe

Try to update your compiler, I'm using GCC 4.7 on Windows 7 Starter x86 with MinGW and it compiles fine with the same options both in C99 and C11.

尝试更新您的编译器,我在带有 MinGW 的 Windows 7 Starter x86 上使用 GCC 4.7,它在 C99 和 C11 中使用相同的选项编译得很好。

回答by bkdm

For portable code, the macros in inttypes.hmay be used. They expand to the correct ones for the platform.

对于可移植代码,可以使用inttypes.h 中的宏。它们扩展到平台的正确版本。

E.g. for 64 bit integer, the macro PRId64can be used.

例如,对于 64 位整数,PRId64可以使用宏。

int64_t n = 7;
printf("n is %" PRId64 "\n", n);

回答by Tommy Wang

You can try settings of code::block, there is a complier..., then you select in C mode.

你可以试试设置的code::block,有编译器...,然后你在C模式下选择。

enter image description here

在此处输入图片说明