【问题标题】:What do 0LL or 0x0UL mean?0LL 或 0x0UL 是什么意思?
【发布时间】:2011-08-12 05:48:21
【问题描述】:

我正在阅读Google Go tutorial,并在常量部分看到了这个:

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

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

【问题讨论】:

  • “我尝试进行 Google 搜索”...尝试搜索词 integer constants c++ LL。 :)
  • 我一直认为它们是“文字”,而常量则是声明为常量的变量。
  • 很公平,但我想integer literals c++ LL 的结果是相似的...... :)

标签: c constants


【解决方案1】:

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

通常,每个Ll 代表一个long,每个Uu 代表一个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

并且没有“usual arithmetic promotion

    var b int32 = 600
    var a int64 = 1000000000 * b
    // ^ cannot use 1000000000 * b (type int32) as type int64 in assignment

所以后缀不是必需的。

【讨论】:

  • 请提供一个使用十六进制文字的示例。例如,什么是 0xFEDCBA9876543210LL(注意 LL 后缀)。它是有符号类型吗(Clang 坚持认为它是无符号类型)?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    0LL 是一个长长的零。

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

    【讨论】:

      【解决方案4】:

      LL 将文字指定为long longUL 将文字指定为unsigned long0x0 是十六进制的0。所以0LL0x0UL 是等价的数字,但数据类型不同;前者是long long,后者是unsigned long

      这些说明符有很多:

      1F // float
      1L // long
      1ull // unsigned long long
      1.0 // double
      

      【讨论】:

      • 在 C 或 C++ 中没有 1D 这样的东西。
      • @Kenny 哦,不是吗? x.x 是否可以达到这个目的?不过感谢您提供的信息,我认为 D 是双倍的。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 2017-06-11
        • 2018-03-05
        相关资源
        最近更新 更多