2011-04-05 152 views
4

編輯刪除的第一個警告%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.

所以這是一個錯誤,肯定?

回答

3
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 << std :: hex << x <<'\ n';' – 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

1

對於第一次警告我可以說你必須使用0xdeadbeefc0defaceLL而不是0xdeadbeefc0deface。之後,其他警告也可以通過。

+0

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

1

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

warning: unknown conversion type character 'l' in format 

所以是的,可能是編譯器/平臺的具體錯誤。

1

這是一個Mingw特有的問題,因爲它調用了本地Windows運行時的某些事情,包括這個。見this answer

%I64d適合我。在上面鏈接的答案中,還有一個更可移植但可讀性更低的解決方案。