%llx格式说明符:无效警告?

问题描述:

编辑删除的第一个警告%llx格式说明符:无效警告?

下面的代码将按预期在G ++ 4.4.0下的mingw32:

#include <cstdio> 
int main() 
    { 
    long long x = 0xdeadbeefc0defaceLL ; 
    printf ("%llx\n", x) ; 
    } 

但是,如果我能够与-Wall所有警告,它说:

f.cpp: In function 'int main()': 
f.cpp:5: warning: unknown conversion type character 'l' in format 
f.cpp:5: warning: too many arguments for format 

这与%lld一样。这是固定在更新的版本?

再次编辑补充:
警告不会消失,如果我指定-std=c++0x,虽然(我)long long是标准型,及(ii)%lld%llx似乎是正式支持。例如,从21.5数字转换第7段:

Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size.

所以这是一个错误,肯定?

long long x = 0xdeadbeefc0defaceLL; // note LL in the end 

并且没有ll长度说明符printf。你可以得到的最好的是:

printf ("%lx\n", x); // l is for long int 

我测试过的样品在我的G ++,它没有错误编译即使没有-std=c++0x标志:

~$ g++ -Wall test.cpp 
~$ g++ --version 
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 

所以,是的,这个固定的更新的版本。

+1

它的标签为C++。你可以得到的最好是'std :: cout MSalters 2011-04-05 07:46:33

+2

是的,对于long long类型没有标准的说明符。但'lld'和'llx'是最便携的。 – 2011-04-05 07:49:13

+1

@ MSalters:现在我已经添加了'C++ 0x'标记,以使'long long'成为标准类型(这就是为什么我将它标记为“C++”,但现在我意识到“long long”不是标准的在'C++ 0x'之前的'C++'中)。 – TonyK 2011-04-05 08:15:28

对于第一次警告我可以说你必须使用0xdeadbeefc0defaceLL而不是0xdeadbeefc0deface。之后,其他警告也可以通过。

+0

谢谢,我现在已经加了'LL'。但其他警告依然存在。 – TonyK 2011-04-05 08:12:03

我得到了相同的警告编译C使用Windows/mingw32。

warning: unknown conversion type character 'l' in format 

所以是的,可能是编译器/平台的具体错误。

这是一个Mingw特有的问题,因为它调用了本地Windows运行时的某些事情,包括这个。见this answer

%I64d适合我。在上面链接的答案中,还有一个更可移植但可读性更低的解决方案。