我正在阅读Google Go教程,并在常量部分中看到了这一点:
没有像0LL或0x0UL这样的常量
我试图进行Google搜索,但所有出现的情况都是人们使用这些常量的情况,但没有解释他们的意思.0x应该启动十六进制文字,但这些不是十六进制数字中可能出现的字符.
ken*_*ytm 30
这些是C和C++中的常量.后缀LL表示常量是类型long long,UL意思是unsigned long.
通常,每个L或l代表一个long和每个U或u代表一个unsigned.所以,例如
1uLL
Run Code Online (Sandbox Code Playgroud)
表示常量1的类型unsigned long long.
这也适用于浮点数:
1.0f // of type 'float'
1.0 // of type 'double'
1.0L // of type 'long double'
Run Code Online (Sandbox Code Playgroud)
和字符串和字符,但它们是前缀:
'A' // of type 'char'
L'A' // of type 'wchar_t'
u'A' // of type 'char16_t' (C++0x only)
U'A' // of type 'char32_t' (C++0x only)
Run Code Online (Sandbox Code Playgroud)
在C和C++中,整数常量使用其原始类型进行计算,这可能会因整数溢出而导致错误:
long long nanosec_wrong = 1000000000 * 600;
// ^ you'll get '-1295421440' since the constants are of type 'int'
// which is usually only 32-bit long, not big enough to hold the result.
long long nanosec_correct = 1000000000LL * 600
// ^ you'll correctly get '600000000000' with this
int secs = 600;
long long nanosec_2 = 1000000000LL * secs;
// ^ use the '1000000000LL' to ensure the multiplication is done as 'long long's.
Run Code Online (Sandbox Code Playgroud)
在Google Go中,所有整数都被评估为大整数(不会发生截断),
var nanosec_correct int64 = 1000000000 * 600
Run Code Online (Sandbox Code Playgroud)
并且没有" 通常的算术推广 "
var b int32 = 600
var a int64 = 1000000000 * b
// ^ cannot use 1000000000 * b (type int32) as type int64 in assignment
Run Code Online (Sandbox Code Playgroud)
所以后缀不是必需的.
有几种不同的基本数字类型,字母区分它们:
0 // normal number is interpreted as int
0L // ending with 'L' makes it a long
0LL // ending with 'LL' makes it long long
0UL // unsigned long
0.0 // decimal point makes it a double
0.0f // 'f' makes it a float
Run Code Online (Sandbox Code Playgroud)