0LL或0x0UL是什么意思?
我正在阅读Google Go教程,并在常量部分中看到了这一点:
没有像0LL或0x0UL这样的常量
我尝试进行Google搜索,但所有出现的情况都是人们使用这些常量的情况,但没有解释他们的意思。 0x应该启动hex文字,但这些不是hex数字中可能出现的字符。
这些是C和C ++中的常量。 后缀LL
表示常量是long long
类型, UL
表示unsigned long
。
通常,每个L
或l
表示long
,每个U
或u
表示unsigned
。 所以,例如
1uLL
表示常量1,类型为unsigned long long
。
这也适用于浮点数:
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 Go中,所有整数都被评估为大整数(不会发生截断),
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
所以后缀不是必需的。
有几种不同的基本数字类型,字母区分它们:
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
0LL
是一个长的长零。
0x0UL
是无符号长零,使用hex表示法表示。 0x0UL
== 0UL
。
+在类C语言中,这些后缀会告诉您确切的类型。 所以,例如。 9是int
变量,但0LL
是long long
LL
将文字指定为long long
, UL
将其指定为unsigned long
整数, 0x0
为0
表示hex。 所以0LL
和0x0UL
是一个等价的数字,但数据类型不同; 前者是long long
,后者是unsigned long
。
有许多这些说明符:
1F // float 1L // long 1ull // unsigned long long 1.0 // double