【问题标题】:%llx format specifier: invalid warning?%llx 格式说明符:无效警告?
【发布时间】:2011-04-05 07:38:37
【问题描述】:

已编辑以删除第一个警告

以下代码在 mingw32 下的 g++ 4.4.0 中按预期工作:

#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,警告不会消失,即使 (i) 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.

所以这肯定是一个错误?

【问题讨论】:

    标签: c++ c++11 g++


    【解决方案1】:
    long long x = 0xdeadbeefc0defaceLL; // note LL in the end
    

    printf 没有 ll 长度说明符。你能得到的最好的是:

    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
    

    所以,是的,这在较新的版本中已修复。

    【讨论】:

    • 它被标记为 C++。你能得到的最好的是std::cout &lt;&lt; std::hex &lt;&lt; x &lt;&lt; '\n';
    • 是的,long long 类型没有标准说明符。但是lldllx 是最便携的。
    • @MSalters:我现在添加了c++0x 标记,以使long long 成为标准类型(这就是我将其标记为C++ 的原因,但现在我意识到long long 是在C++ C++0x 之前不是标准的)。
    【解决方案2】:

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

    【讨论】:

    • 谢谢,我现在添加了LL。但其他警告仍然存在。
    【解决方案3】:

    我在使用 windows/mingw32 编译 C 时收到相同的警告。

    warning: unknown conversion type character 'l' in format
    

    是的,可能是编译器/平台特定的错误。

    【讨论】:

      【解决方案4】:

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

      %I64d 为我工作。在上面链接的答案中,有一个更便携但可读性较差的解决方案。

      【讨论】:

        猜你喜欢
        • 2018-10-30
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多