#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?

有帮助吗?

解决方案

将 ll (el-el) long-long 修饰符与 u(无符号)转换结合使用。(适用于 Windows、GNU)。

printf("%llu", 285212672);

其他提示

您可能想尝试使用 inttypes.h 库,它为您提供了诸如int32_t, int64_t, uint64_t ETC。然后您可以使用它的宏,例如:

uint64_t x;
uint32_t y;

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

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

%d--> 对于 int

%u--> 对于 unsigned int

%ld--> 对于 long int

%lu--> 对于 unsigned long int

%lld--> 对于 long long int

%llu--> 对于 unsigned long long int

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

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

这是因为 %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.

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

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

使用 VS2005 将其编译为 x64:

%llu 运行良好。

非标准的东西总是很奇怪:)

对于GNU下的长期长部分 L, ll 或者 q

在Windows下我相信它是 ll 仅有的

十六进制:

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

输出:

64bit: FFFFFFFFFFFFFFFF

除了人们多年前写的内容之外:

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

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

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

那么你的mingw版本默认不是c99。添加此编译器标志: -std=c99.

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

显然没有人提出一种多平台*解决方案 十多年来 自 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().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top