如何使用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;
}

输出: windows

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

我认为这种意外的结果是因为打印unsigned long long int 。 你如何printf()一个unsigned long long intapi


#1楼

使用VS2005将其编译为x64: ide

%llu运做良好。 spa


#2楼

对于使用MSVS的很长一段时间(或__int64),应该使用%I64d: code

__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b);    //I is capital i

#3楼

将ll(el-el)long-long修饰符与u(无符号)转换一块儿使用。 (在Windows,GNU中运行)。 orm

printf("%llu", 285212672);

#4楼

非标准的东西老是很奇怪:) 编译器

对于GNU下的long long部分,它是Lllq it

和windows下我相信这是llio


#5楼

好吧,一种方法是使用VS2008将其编译为x64 编译

这将按您指望的那样运行:

int normalInt = 5; 
unsigned long long int num=285212672;
printf(
    "My number is %d bytes wide and its value is %ul. 
    A normal number is %d \n", 
    sizeof(num), 
    num, 
    normalInt);

对于32位代码,咱们须要使用正确的__int64格式说明符%I64u。 就这样。

int normalInt = 5; 
unsigned __int64 num=285212672;
printf(
    "My number is %d bytes wide and its value is %I64u. 
    A normal number is %d", 
    sizeof(num),
    num, normalInt);

此代码适用于32位和64位VS编译器。

相关文章
相关标签/搜索