28

我正在阅读Google Go 教程,并在常量部分看到了这一点:

没有像 0LL 或 0x0UL 这样的常量

我尝试进行谷歌搜索,但出现的只是人们使用这些常量但没有解释它们的含义的实例。0x 应该以十六进制文字开头,但这些不是十六进制数字中可能出现的字符。

4

5 回答 5

41

这些是 C 和 C++ 中的常量。后缀LL表示常量是类型long longUL表示unsigned long

通常,每个Lorl代表 along并且每个Uoru代表一个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

所以后缀不是必需的。

于 2011-08-12T05:50:27.487 回答
10

有几种不同的基本数字类型,字母区分它们:

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-12T05:52:20.970 回答
4

0LL是一个长长的零。

0x0UL是一个无符号长零,使用十六进制表示法表示。 0x0UL== 0UL

于 2011-08-12T05:49:54.353 回答
3

LL将文字指定为 along long并将其UL指定为unsigned long并且0x0是十六进制的0. 所以0LL0x0UL是等价的数字,但数据类型不同;前者是a long long,后者是a unsigned long

这些说明符有很多:

1F // float
1L // long
1ull // unsigned long long
1.0 // double
于 2011-08-12T05:50:35.467 回答
2

+在类 C 语言中,这些后缀告诉您确切的类型。所以,例如。9 是一个变量int,但0LLlong long

于 2011-08-12T05:50:50.347 回答