如何使用printf格式化unsigned long long int?

#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.\n", sizeof(num), num, normalInt);

    return 0;

}

输出:


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

我认为此意外结果是由于打印unsigned long long int。你怎么printf()了unsigned long long int?


喵喵时光机
浏览 3070回答 3
3回答

慕虎7371278

将ll(el-el)long-long修饰符与u(无符号)转换一起使用。(在Windows,GNU中运行)。printf("%llu", 285212672);

跃然一笑

您可能需要使用inttypes.h库,让你的类型,如尝试 int32_t,int64_t,uint64_t等等,那么你可以使用它的宏,例如:uint64_t x;uint32_t y;printf("x: %"PRId64", y: %"PRId32"\n", x, y);这是“保证”不给你同样的麻烦的long,unsigned long long等等,因为你不必去猜测有多少位是每个数据类型。
打开App,查看更多内容
随时随地看视频慕课网APP