Dev-C++下關於long long類型的實驗

來源:互聯網
上載者:User

kingwei 2005.3.10

實驗環境: Dev-C++ 4.9.6.0 (gcc/mingw32), 使用-Wall編譯選項

#include <stdio.h>

int main()
{
 signed long long int v_signed_long_long_int;
 unsigned long long int v_unsigned_long_long_int;
  
 /* PART1:USE %I64d AND %I64u */
 
 /* [-2^63, 2^63-1] ==> [-9223372036854775808, 9223372036854775807] */
 scanf("%I64d", &v_signed_long_long_int); 
 printf("%I64d/n", v_signed_long_long_int);

 /* [0, 2^64-1] ==> [0, 18446744073709551615]  */
 scanf("%I64u", &v_unsigned_long_long_int);
 printf("%I64u/n", v_unsigned_long_long_int);

 /* PART2:USE %lld AND %llu */
 
 /* [-2^63, 2^63-1] ==> [-9223372036854775808, 9223372036854775807] */
 scanf("%lld", &v_signed_long_long_int); 
 printf("%lld/n", v_signed_long_long_int);

 /* [0, 2^64-1] ==> [0, 18446744073709551615]  */
 scanf("%llu", &v_unsigned_long_long_int);
 printf("%llu/n", v_unsigned_long_long_int);
 
 return 0;
}

這個程式在Dev-C++下編譯會有一系列Warning:

   D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] In function `int main()':
14 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] unknown conversion type character `I' in format
14 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] too many arguments for format
15 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] unknown conversion type character `I' in format
15 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] too many arguments for format
18 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] unknown conversion type character `I' in format
18 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] too many arguments for format
19 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] unknown conversion type character `I' in format
19 D:/Dev-C++下關於long long類型的實驗/llint_format.cpp [Warning] too many arguments for format

可以看到,Warning都是因為PART1中使用了格式符%I,而產生的。PART2則一切正常,編譯器沒有給出任何警告資訊。

但是——請看測試結果:

----- test case #1: 下界 -----

-9223372036854775808
0
-9223372036854775808
0

output:

-9223372036854775808
0
0
0

----- test case #2: 上界 -----

9223372036854775807
18446744073709551615
9223372036854775807
18446744073709551615

output:

9223372036854775807
18446744073709551615
-1
4294967295

----- test case #3: 下溢 -----

-9223372036854775809
-1
-9223372036854775809
-1

output:

9223372036854775807
18446744073709551615
-1
4294967295

----- test case #4: 上溢 -----

9223372036854775808
18446744073709551616
9223372036854775808
18446744073709551616

output:

-9223372036854775808
0
0
0

結果恰恰相反,PART1的工作完全正常,PART2卻產生了問題.
為什麼呢?我是這樣猜想的:

"%lld"和"%llu"是linux下gcc/g++用於long long int類型(64 bits)輸入輸出的格式符。

而"%I64d"和"%I64u"則是Microsoft VC++庫裡用於輸入輸出__int64類型的格式說明。

問題就出在:Dev-C++使用的編譯器是Mingw32,Mingw32是x86-win32 gcc子項目之一,編譯器核心還是linux下的gcc。

進行函數參數類型檢查的是在編譯階段,gcc編譯器對這段代碼進行檢查,顯然它不認得"%I64d",
所以給出了警告“unknown conversion type character `I' in format”,它不認為這是一個格式說明符。
接著,gcc發現整個格式字串裡沒有合法的說明符,但傳給printf/scanf的除了格式字串,還有資料參數,
於是它又給出了第二個警告“too many arguments for format”。

對於"%lld"和"%llu",gcc理所當然地接受了。悲劇就此埋下了伏筆。

Mingw32在編譯期間使用gcc的規則檢查文法,在串連和運行時使用的卻是Microsoft庫。
這個庫裡的printf和scanf函數當然不認識linux gcc下"%lld"和"%llu",對"%I64d"和"%I64u",它則是樂意接受的。

——於是就出現了上面的結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.