格式化 long 的 printf 的转换说明符是什么?
printf
函数采用参数类型,例如 %d
或 %i
表示 signed int
。 但是,我没有看到任何 long
值。
The printf
function takes an argument type, such as %d
or %i
for a signed int
. However, I don't see anything for a long
value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想你的意思是:
或者
I think you mean:
or
在大多数平台上,
long
和int
的大小相同(32 位)。 尽管如此,它确实有自己的格式说明符:对于 64 位,您需要一个
long long
:哦,当然,它在 Windows 中有所不同:
经常,当我打印 64 位时值,我发现以十六进制打印它们很有帮助(通常数字很大,它们是指针或位字段)。
将打印:
顺便说一句,“长”不再意味着那么多(在主流 x64 上)。 “int”是平台默认的 int 大小,通常为 32 位。 “长”通常是相同的尺寸。 然而,它们在旧平台(和现代嵌入式平台!)上具有不同的可移植性语义。 “long long”是一个 64 位数字,通常是人们想要使用的数字,除非他们真的知道他们正在编辑一段 x 平台可移植代码。 即使如此,他们也可能会使用宏来捕获类型的语义(例如 uint64_t)。
过去,“int”是 16 位。 您可能会认为现在是 64 位,但事实并非如此,这会导致严重的可移植性问题。 当然,这也是对神秘且历史悠久的事实的简化。 请参阅 wiki:Integer
On most platforms,
long
andint
are the same size (32 bits). Still, it does have its own format specifier:For 64 bits, you'd want a
long long
:Oh, and of course, it's different in Windows:
Frequently, when I'm printing 64-bit values, I find it helpful to print them in hex (usually with numbers that big, they are pointers or bit fields).
will print:
Btw, "long" doesn't mean that much anymore (on mainstream x64). "int" is the platform default int size, typically 32 bits. "long" is usually the same size. However, they have different portability semantics on older platforms (and modern embedded platforms!). "long long" is a 64-bit number and usually what people meant to use unless they really really knew what they were doing editing a piece of x-platform portable code. Even then, they probably would have used a macro instead to capture the semantic meaning of the type (eg uint64_t).
Back in the day, "int" was 16 bits. You'd think it would now be 64 bits, but no, that would have caused insane portability issues. Of course, even this is a simplification of the arcane and history-rich truth. See wiki:Integer
这取决于,如果您指的是 unsigned long,则格式化字符为
"%lu"
。 如果您指的是有符号长整型,则格式字符为"%ld"
。It depends, if you are referring to unsigned long the formatting character is
"%lu"
. If you're referring to signed long the formatting character is"%ld"
.%ld
请参阅printf
参考在 cplusplus.com 上%ld
seeprintf
reference on cplusplus.com我需要打印
unsigned long long
,所以我发现这有效:对于所有其他组合,我相信您使用 printf 手册,先获取行,然后获取要打印的任何类型的列标签(就像我使用
printf("%llu", n) 所做的那样上面)。
I needed to print
unsigned long long
, so I found this works:For all other combinations, I believe you use the table from the printf manual, taking the row, then column label for whatever type you're trying to print (as I do with
printf("%llu", n)
above).我认为要明确回答这个问题需要知道您正在使用的编译器名称和版本以及它正在编译的平台(CPU 类型、操作系统等)。
I think to answer this question definitively would require knowing the compiler name and version that you are using and the platform (CPU type, OS etc.) that it is compiling for.
将
l
(小写字母 L)直接放在说明符之前。Put an
l
(lowercased letter L) directly before the specifier.