Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c - Mmh, who are you PRIu64?

I am new to C and I am confronted with:

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

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

    return 0;
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

PRIu64 is a string (literal), for example the following:

printf("%s
", PRIu64);

prints llu on my machine. Adjacent string literals are concatenated, from section 6.4.5 String literals of the C99 standard:

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.

This means:

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

(on my machine) is the same as:

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

See http://ideone.com/jFvKR9 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...