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

时间:2008-08-05 20:59:29

标签: c syntax printf format-specifiers long-long

#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

13 个答案:

答案 0 :(得分:427)

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

printf("%llu", 285212672);

答案 1 :(得分:83)

您可能想尝试使用为您提供类型的inttypes.h库 int32_tint64_tuint64_t等 然后,您可以使用其宏,例如:

uint64_t x;
uint32_t y;

printf("x: %"PRId64", y: %"PRId32"\n", x, y);

“保证”不会给您带来与longunsigned long long等相同的麻烦,因为您不必猜测每种数据类型中有多少位。

答案 2 :(得分:55)

%d - &GT; int

%u - &GT; unsigned int

%ld - &GT; long int

%lu - &GT; unsigned long int

%lld - &GT; long long int

%llu - &GT; unsigned long long int

答案 3 :(得分:37)

对于使用MSVS的long long(或__int64),您应该使用%I64d:

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

答案 4 :(得分:35)

这是因为%llu在Windows下无法正常工作,%d无法处理64位整数。我建议改用PRIu64,你会发现它也可以移植到Linux上。

请改为尝试:

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

int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    /* NOTE: PRIu64 is a preprocessor macro and thus should go outside the quoted string. */
    printf("My number is %d bytes wide and its value is %" PRIu64 ". A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}

输出

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

答案 5 :(得分:12)

在Linux中,它是%llu,在Windows中是%I64u

虽然我发现它在Windows 2000中不起作用,但似乎有一个错误!

答案 6 :(得分:8)

使用VS2005将其编译为x64:

  

%llu效果很好。

答案 7 :(得分:4)

非标准事物总是很奇怪:)

长的部分 在GNU下,它是Lllq

在Windows下我认为只有ll

答案 8 :(得分:4)

十六进制:

printf("64bit: %llp", 0xffffffffffffffff);

输出:

64bit: FFFFFFFFFFFFFFFF

答案 9 :(得分:2)

除了几年前人们写的内容:

  • 您可能会在gcc / mingw上收到此错误:

main.c:30:3: warning: unknown conversion type character 'l' in format [-Wformat=]

printf("%llu\n", k);

然后您的mingw版本不会默认为c99。添加此编译器标志:-std=c99

答案 10 :(得分:1)

<块引用>

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

从 C99 开始,在转换说明符 "ll" 之前使用 o,u,x,X (ell-ell)。

许多答案中除了基数为 10 的选项外,还有基数为 16 和基数为 8 的选项:

选择包括

unsigned long long num = 285212672;
printf("Base 10: %llu\n", num);
num += 0xFFF; // For more interesting hex/octal output.
printf("Base 16: %llX\n", num); // Use uppercase A-F
printf("Base 16: %llx\n", num); // Use lowercase a-f
printf("Base  8: %llo\n", num);
puts("or 0x,0X prefix");
printf("Base 16: %#llX %#llX\n", num, 0ull); // When non-zero, print leading 0x
printf("Base 16: %#llx %#llx\n", num, 0ull);
printf("Base 16: 0x%llX\n", num); // My hex fave: lower case prefix, with A-F

输出

Base 10: 285212672
Base 16: 11000FFF
Base 16: 11000fff
Base  8: 2100007777
or 0x,0X prefix
Base 16: 0X11000FFF 0
Base 16: 0x11000fff 0
Base 16: 0x11000FFF

答案 11 :(得分:0)

嗯,一种方法是使用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编译器。

答案 12 :(得分:-1)

很明显,自2008年以来,没有人针对十年来提出过多平​​台*的解决方案,因此我将附上我的文章。请投票。 (开玩笑。我不在乎。)

解决方案:lltoa()

使用方法:

#include <stdlib.h> /* lltoa() */
// ...
char dummy[255];
printf("Over 4 bytes: %s\n", lltoa(5555555555, dummy, 10));
printf("Another one: %s\n", lltoa(15555555555, dummy, 10));

OP的示例:

#include <stdio.h>
#include <stdlib.h> /* lltoa() */

int main() {
    unsigned long long int num = 285212672; // fits in 29 bits
    char dummy[255];
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %s. "
        "A normal number is %d.\n", 
        sizeof(num), lltoa(num, dummy, 10), normalInt);
    return 0;
}

%lld打印格式字符串不同,此选项对我在Windows上的32位GCC下有效。

*)好吧,几乎多平台。在MSVC中,您显然需要_ui64toa()而不是lltoa()