Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用printf格式化无符号长整型?_C_Syntax_Printf_Format Specifiers_Long Long - Fatal编程技术网

C 如何使用printf格式化无符号长整型?

C 如何使用printf格式化无符号长整型?,c,syntax,printf,format-specifiers,long-long,C,Syntax,Printf,Format Specifiers,Long Long,我假设这个意外的结果来自于打印无符号long-long-int。如何printf()aunsigned long-long int?将ll(el)long-long修饰符与u(unsigned)转换一起使用。(在windows、GNU中工作) 非标准的东西总是很奇怪:) 长份 在GNU下是L,ll或q 在windows下,我相信它是ll唯一的好吧,一种方法是用VS2008将它编译成x64 这与您预期的一样: printf("%llu", 285212672); 对于32位代码,我们需要使用正确

我假设这个意外的结果来自于打印
无符号long-long-int
。如何
printf()
a
unsigned long-long int

将ll(el)long-long修饰符与u(unsigned)转换一起使用。(在windows、GNU中工作)


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

长份 在GNU下是
L
ll
q


在windows下,我相信它是
ll
唯一的好吧,一种方法是用VS2008将它编译成x64

这与您预期的一样:

printf("%llu", 285212672);
对于32位代码,我们需要使用正确的uu int64格式说明符%I64u。就这样

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位和64位VS编译器。

您可能需要尝试使用inttypes.h库,该库为您提供以下类型:
int32\u t
int64\u t
uint64\u t
等。 然后可以使用其宏,例如:

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);

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

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

请尝试以下方法:

uint64_t x;
uint32_t y;

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

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

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

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

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

使用VS2005将其编译为x64:

%llu运作良好

%d
-->对于
int

%u
-->对于
无符号整数

%ld
-->用于
long int
long

%lu
-->用于
无符号长整数
无符号长整数
无符号长整数

%lld
-->用于
long-long-int
long-long

%llu
-->对于
无符号长整型
无符号长整型
十六进制:

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

#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;
}
printf("64bit: %llp", 0xffffffffffffffff);

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

  • 在gcc/mingw上可能会出现以下错误:
main.c:30:3:警告:格式为[-Wformat=]的未知转换类型字符“l”

printf(“%llu\n”,k)


那么您的mingw版本不会默认为c99。添加此编译器标志:
-std=c99
显然,自2008年以来,十多年来,没有人提出多平台*解决方案,因此我将添加我的解决方案 如何使用
printf
格式化
无符号long-long-int

因为C99在转换说明符
o,u,x,x之前使用了
“ll”
(ell-ell)

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

选择包括

64bit: FFFFFFFFFFFFFFFF
输出

#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;
}
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

或者更准确地说,它是针对GNU libc的,不适用于Microsoft的C运行时。这不是Linux/UNIX的事情,C99中的标准C中添加了“ll”长度修饰符,如果它在“Microsoft C”中不起作用,那是因为它们不符合标准。在VS2008中适用于我。此外,据我记忆所及,MS C编译器(当设置为编译纯C时)在设计上应该与C90兼容;C99介绍了一些并非所有人都喜欢的东西。这里需要记住的一点是,如果您将多个
long
参数传递给
printf
,并对其中一个参数使用错误的格式,比如说
%d
而不是
%lld
,那么即使是在错误参数之后打印的参数也可能会完全关闭(甚至可能导致
printf
崩溃)本质上,变量参数传递给Primtf,没有任何类型信息,所以如果格式字符串不正确,结果是不可预测的。Edter Heor萨特在一次采访中说,微软的客户不要求C99,所以他们的纯C编译器在C90中被冻结。如果编译为C++,则编译为C。其他人已经在上面提到过,你应该很好。使用windows(或者至少使用microsoft C编译器for windows)还有%I64d、%I32u和%i32d这与Windows 2000有什么关系?C库是处理printf的库。正如我所观察到的。我编写了一个应用程序,它使用这种结构,在WinXP上运行得很好,但在Win2k上却有垃圾。也许这与C库对内核进行的系统调用有关,也许是与Unicode有关,谁知道呢。我记得必须使用_i64tot()或类似的东西来解决它。听起来像是MS再次行使其“创新自由”;-)Win2k/Win9x问题可能是由于
无符号长时间
数据类型相对较新(当时是C99标准),而C编译器(包括MinGW/GCC)利用只支持C89规范的旧Microsoft C运行时。我只能访问非常旧和合理的最新Windows API文档。因此很难说什么时候加入了
I64u
支持。但这听起来像是XP时代。+1用于参考PRIu64,我从未见过,但这似乎不便于移植64位Linux(至少),因为PRIu64扩展为“lu”而不是“llu”。那为什么不好呢?在64位Linux上,long是一个64位的值,就像在除Windows以外的所有其他操作系统上一样。@BDatRivenhill Linux/Unix使用LP64,其中long是64位的showever,为了使它更具可移植性,请使用
int64_t
,因为可能有一些实现的long-long比long大这应该是最重要的!-一个small update:错误:文本上的后缀无效;C++11要求文本和标识符[-Wreserved user defined literal]之间有空格我刚刚用gcc编译了您的代码(使用%llu),输出结果是正确的。您是pas吗
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