2016-10-29 23 views
2

如果我想bi是一個long int,是不可能使用自動,因爲它總是分配爲int?當我使用自動bi = 123456789,在C++中,它總是被分配爲一個int?

+1

爲什麼你認爲它「賦值爲int」? – juanchopanza

+4

如果你用引號包裝它,它絕對不會是一個整數。這將是一個'char const *'(我認爲)。 ---你可以像上面提到的[這裏]檢查變量的類型(http://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in​​-standard-c ) – byxor

+2

如果你特別想讓它成爲'long',那麼爲什麼不直接給編譯器'long bi ='說呢? –

回答

11

一些選項:以上

auto bi = "123456789";   // const char* 
auto bi2 = 12345;    // int 
auto bi3 = 123456789;   // int (when int is 32 bits or more) 
auto bi4a = 123456789L;   // long 
auto bi4b = 178923456789L;  // long long! (L suffix asked for long, but got long long so that the number can fit) 
auto bi5a = 123456789LL;  // long long 
auto bi5b = 123456784732899; // long long (on my system it is long long, but might be different on ILP64; there is would just be an int) 
auto bi6 = 123456789UL;   // unsigned long 
auto bi7 = 123456789ULL;  // unsigned long long 

所有的例子取決於您所使用的系統上。

在標準中,在[lex.icon]表5 - 整數常量的類型被引用:

類型字面的整數的是表5中的第一相應列表 ,其中它的值可以被代表。

如果我們看一下表十進制文本我們看到UL後綴的甚至影響取決於可容納多大尺寸:

enter image description here

+0

夢幻般的答案。你可以更進一步並添加一個「long long」的例子。 – byxor

+1

也longlong(ll)和unsigned long long(ull)。 – Robinson

+0

我試圖找到一個SO問題,其中表格是從描述如何爲文字選擇類型的標準提及的。 – wally

相關問題