#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

===============>>#1 票数:457 已采纳

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

printf("%llu", 285212672);

===============>>#2 票数:84

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

uint64_t x;
uint32_t y;

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

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

===============>>#3 票数:62

%d >用于int

%u >用于unsigned int

%ld >用于long int

%lu >表示unsigned long int

%lld > for long long int

%llu >对于unsigned long long int

===============>>#4 票数:38

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

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

===============>>#5 票数:36

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

===============>>#6 票数:12

在Linux中为%llu ,在Windows中为%I64u

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

===============>>#7 票数:8

使用VS2005将其编译为x64:

%llu运作良好。

===============>>#8 票数:4

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

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

和windows下我相信这是ll

===============>>#9 票数:4

十六进制:

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

输出:

64bit: FFFFFFFFFFFFFFFF

===============>>#10 票数:2

除了人们几年前写的东西:

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

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

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

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

===============>>#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 票数:0

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

解决方案: 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()

  ask by andrewrk translate from so

未解决问题?本站智能推荐: