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
533 views
in Technique[技术] by (71.8m points)

c - 如何使用printf格式化unsigned long long int?(How do you format an unsigned long long int using printf?)

#include <stdio.h>
int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %ul. A normal number is %d.
", sizeof(num), num, normalInt);
    return 0;
}

Output:

(输出:)

My number is 8 bytes wide and its value is 285212672l. A normal number is 0.

I assume this unexpected result is from printing the unsigned long long int .

(我认为这种意外的结果是由于打印unsigned long long int 。)

How do you printf() an unsigned long long int ?

(你如何printf()一个unsigned long long int ?)

  ask by andrewrk translate from so

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

1 Reply

0 votes
by (71.8m points)

Use the ll (el-el) long-long modifier with the u (unsigned) conversion.

(将ll(el-el)long-long修饰符与u(无符号)转换一起使用。)

(Works in windows, GNU).

((在Windows,GNU中运行)。)

printf("%llu", 285212672);

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

...