数值文字上的 ULL 后缀

我遇到过这样的代码:

line += addr & 0x3fULL;

显然,‘ U’和‘ L’不是十六进制数字。我猜那个十六进制数字字面值后面的‘ ULL’意思是“无符号的长长的”——我说的对吗?(这种事情是非常困难的谷歌)如果是这样,那么这是一些后缀修饰的数字排序?

103497 次浏览

Yes that's correct.

  • 0x prefix makes it a hexadecimal literal.
  • ULL suffix makes it type unsigned long long.

From the gcc manual:

ISO C99 supports data types for integers that are at least 64 bits wide ( . . . ) . To make an integer constant of type long long int, add the suffix LL to the integer. To make an integer constant of type unsigned long long int, add the suffix ULL to the integer.

These suffixes have also been added to C++ in C++11, and were already supported long long (pun intended) before that as compiler extensions.

I'm positing a new answer because I recognize that the current answers do not cite from a cross platform source. The standard dictates that a literal with ABC0/ABC1 and ABC2/ABC3 suffixes is a literal of type: unsigned long long int [source]

U/u is the C/C++ suffix for an unsigned integer.
LL/ll is the C/C++ suffix for a long long integer which is a new type in C++11 and required to have a length of at least 64-bits.

Notes:

  1. The keyword int may be omitted if any modifiers are used, unsigned long long for example. So this will define one as an unsigned long long int, and any number assigned to it will be static_cast to unsigned long long int: unsigned long long one = 1
  2. marked the advent of auto. Which sets the variable type to the type assigned to it on declaration. For example, because 2ULL is an unsigned long long int literal two will be defined as an unsigned long long int: auto two = 2ULL
  3. introduced order independent literal suffixes. Previously the U/u suffix had to preceded any size suffix. But circa the suffixes are accepted in either order, so now since 3LLU is an unsigned long long int literal three will be defined as an unsigned long long int: auto three = 3LLU