我正在阅读Google教程,并在常量部分中看到了这一点:
没有像0LL或0x0UL这样的常量
我试图做谷歌搜索,但所有出现的情况是,人们使用这些常量,但没有解释他们的意思。0x应该启动十六进制文字,但这些字符不可能以十六进制数表示。
发布于 2011-08-12 05:50:27
这些是C和C++中的常量。后缀LL
表示常量是long long
类型,UL
表示unsigned long
。
一般来说,每个L
或l
代表一个long
,每个U
或u
代表一个unsigned
。因此,例如。
1uLL
表示类型为unsigned long long
的常量1。
这也适用于浮点数:
1.0f // of type 'float'
1.0 // of type 'double'
1.0L // of type 'long double'
以及字符串和字符,但它们是前缀:
'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)
在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.
在Google中,所有整数都被计算为大整数(不发生截断),
var nanosec_correct int64 = 1000000000 * 600
也没有"常用算术推广“
var b int32 = 600
var a int64 = 1000000000 * b
// ^ cannot use 1000000000 * b (type int32) as type int64 in assignment
所以后缀是不必要的。
发布于 2011-08-12 05:52:20
有几种不同的基本数字类型,字母区分它们:
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
发布于 2011-08-12 05:49:54
0LL
是一个长的零。
0x0UL
是一个无符号长零,用十六进制表示法表示。0x0UL
== 0UL
.
https://stackoverflow.com/questions/7036056
复制相似问题