简体   繁体   English

“长期无符号”在C中是否与“unsigned long”一样有效?

[英]Is 'long unsigned' as valid as 'unsigned long' in C?

A question was recently asked about whether ULL or LLU was valid for specifying unsigned long long constants in C. I know they're both valid but I was thinking that ULL would be preferable since it matched the type unsigned long long . 最近有一个问题是关于ULLLLU是否有效用于指定C中的无符号长long常量。我知道它们都是有效的但我认为ULL会更好,因为它匹配unsigned long long类型。

However, I'm not so certain now. 但是,我现在不太确定。 The gcc compiler allowed the following without complaint: gcc编译器允许以下内容无怨言:

int main(void) {
    unsigned int a = 1;
    int unsigned b = 2;
    return 0;
}

So my question is: are int unsigned , and other variations like long long unsigned , valid types according to the standard? 所以我的问题是: int unsigned ,还有其他变体,比如根据标准的long long unsigned有效类型?

The ISO C11 standard states in 6.2.5 Types : ISO C11标准在6.2.5 Types

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. 有五种标准的有符号整数类型,指定为signed char,short int,int,long int和long long int。

For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned ) that uses the same amount of storage (including sign information) and has the same alignment requirements. 对于每个有符号整数类型,存在相应的(但不同的)无符号整数类型(使用关键字unsigned指定),它使用相同数量的存储(包括符号信息)并具有相同的对齐要求。

However there's no mandate in that section as to the order in which the base type and unsigned modifier appears. 但是,在该部分中没有关于基本类型和unsigned修饰符出现的顺序的任务。

The controlling section is later in the standard, 6.7.2 Type specifiers , paraphrased here: 控制部分稍后在标准中, 6.7.2 Type specifiers ,在此解释:

Type specifiers are void , char , short , int , long , float , double , signed , unsigned , _Bool , _Complex , <struct-or-union-specifier> , <enum-specifier> , and <typedef-name> . 类型说明符是voidcharshortintlongfloatdoublesignedunsigned_Bool_Complex<struct-or-union-specifier><enum-specifier><typedef-name>

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. 每个声明中的声明说明符中应至少给出一个类型说明符,并在每个结构声明和类型名称的说明符限定符列表中给出。 Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); 每个类型说明符列表应为以下多个集合之一(以逗号分隔,每个项目有多个多集); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers. 类型说明符可以按任何顺序出现,可能与其他声明说明符混合。

It then goes on to list all the multisets, such as unsigned long, or unsigned long int . 然后继续列出所有多重集,例如unsigned long, or unsigned long int

But the important phrase there is the type specifiers may occur in any order , meaning that all of these are valid for that multiset: 但是重要的短语是the type specifiers may occur in any order ,这意味着所有这些对于该multiset都有效:

unsigned long
long unsigned

unsigned long int
unsigned int long
long unsigned int
long int unsigned
int unsigned long
int long unsigned

The order of the specifier dosen't matter. 说明符的顺序无关紧要。

unsigned long long  is the same as long long unsigned.  

Both types are valid according to C standard(c99). 两种类型均符合C标准(c99)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM