C语言 0LL 或 0x0UL 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7036056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:22:16  来源:igfitidea点击:

What do 0LL or 0x0UL mean?

cconstants

提问by Bjorn

I am reading the Google Go tutorialand saw this in the constants section:

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

There are no constants like 0LL or 0x0UL

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

I tried to do a Google search but all that comes up are instances where people are using these constants but no explanation as to what they mean. 0x is supposed to start a hexadecimal literal but these are not characters that are possible in a hexadecimal number.

我试图进行谷歌搜索,但所有出现的情况都是人们使用这些常量但没有解释它们的含义。0x 应该开始一个十六进制文字,但这些不是十六进制数字中可能出现的字符。

回答by kennytm

These are constants in C and C++. The suffix LLmeans the constant is of type long long, and ULmeans unsigned long.

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

In general, each Lor lrepresents a longand each Uor urepresents an unsigned. So, e.g.

一般来说,每个Ll代表一个long,每个Uu代表一个unsigned。所以,例如

1uLL

means the constant 1 with type unsigned long long.

表示类型为 的常量 1 unsigned long long

This also applies to floating point numbers:

这也适用于浮点数:

1.0f    // of type 'float'
1.0     // of type 'double'
1.0L    // of type 'long double'

and strings and characters, but they are prefixes:

和字符串和字符,但它们是前缀:

 '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)


In C and C++ the integer constants are evaluated using their original type, which can cause bugs due to integer overflow:

在 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.

In Google Go, all integers are evaluated as big integers (no truncation happens),

在 Google Go 中,所有整数都被评估为大整数(不会发生截断),

    var nanosec_correct int64 = 1000000000 * 600

and there is no "usual arithmetic promotion"

并且没有“通常的算术提升

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

so the suffixes are not necessary.

所以后缀不是必需的。

回答by Tim

There are several different basic numeric types, and the letters differentiate them:

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

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

回答by wallyk

0LLis a long long zero.

0LL是一个长长的零。

0x0ULis an unsigned long zero, expressed using hexadecimal notation. 0x0UL== 0UL.

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

回答by Seth Carnegie

LLdesignates a literal as a long longand ULdesignates one as unsigned longand 0x0is hexadecimal for 0. So 0LLand 0x0ULare an equivalent number but different datatypes; the former is a long longand the latter is an unsigned long.

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

There are many of these specifiers:

这些说明符有很多:

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

回答by Charlie Martin

+In C-like languages, those suffixes tell you the exact type. So, for example. 9 is an intvariable, but 0LLis a long long

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