C语言 嗯,你是谁 PRIu64?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16859500/
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 06:31:22  来源:igfitidea点击:

Mmh, who are you PRIu64?

cmacrosprintf

提问by torr

I am new to C and I am confronted with:

我是 C 的新手,我面临着:

#include <stdio.h>
#include <inttypes.h>

int main(void)
{
    uint64_t foo = 10;
    printf("foo is equal to %" PRIu64 "!\n", foo);

    return 0;
}

And it works! I don't understand why? Can somebody help me about this? Thanks a lot! torr

它有效!我不明白为什么?有人可以帮我解决这个问题吗?非常感谢!托

回答by hmjd

PRIu64is a format specifier, introduced in C99, for printing uint64_t, where uint64_tis (from linked reference page):

PRIu64是 C99 中引入的格式说明符,用于打印uint64_t,其中uint64_t是(来自链接的参考页面):

unsigned integer type with width of ... 64 bits respectively (provided only if the implementation directly supports the type)

宽度分别为 ... 64 位的无符号整数类型(仅当实现直接支持该类型时才提供)

PRIu64is a string (literal), for example the following:

PRIu64是一个字符串(文字),例如以下内容:

printf("%s\n", PRIu64);

prints lluon my machine. Adjacent string literals are concatenated, from section 6.4.5 String literalsof the C99 standard:

llu在我的机器上打印。连接相邻的字符串文字,来自C99 标准的第6.4.5字符串文字

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.

在翻译阶段 6 中,由相邻字符和宽字符串文字标记的任何序列指定的多字节字符序列连接成单个多字节字符序列。如果任何标记是宽字符串文字标记,则生成的多字节字符序列将被视为宽字符串文字;否则,它被视为字符串文字。

This means:

这意味着:

printf("foo is equal to %" PRIu64 "!\n", foo);

(on my machine) is the same as:

(在我的机器上)与:

printf("foo is equal to %llu!\n", foo);

See http://ideone.com/jFvKR9.

请参阅http://ideone.com/jFvKR9