'long unsigned' 和 C 中的 'unsigned long' 一样有效吗? [英] Is 'long unsigned' as valid as 'unsigned long' in C?

查看:17
本文介绍了'long unsigned' 和 C 中的 'unsigned long' 一样有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题最近被问到是否ULLLLU 可用于在 C 中指定 unsigned long long 常量.我知道它们都是有效的,但我认为 ULL 会更可取,因为它与 unsigned long long.

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.

但是,我现在不太确定.gcc 编译器毫无异议地允许以下内容:

However, I'm not so certain now. The gcc compiler allowed the following without complaint:

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

所以我的问题是:int unsigned 和其他变体如 long long long unsigned 是否符合标准的有效类型?

So my question is: are int unsigned, and other variations like long long unsigned, valid types according to the standard?

推荐答案

ISO C11 标准在 6.2.5 Types 中声明:

The ISO C11 standard states in 6.2.5 Types:

有五种标准的有符号整数类型,分别指定为signed char、short int、int、long int和long long int.

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int.

对于每一种有符号整数类型,都有一个对应的(但不同的)无符号整数类型(用关键字unsigned指定),它使用相同的存储量(包括符号信息)并具有相同的对齐要求.

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.

控制部分在标准的后面,6.7.2 类型说明符,解释如下:

The controlling section is later in the standard, 6.7.2 Type specifiers, paraphrased here:

类型说明符有 voidcharshortintlongfloatdoublesignedunsigned_Bool_Complex.

Type specifiers are void, char, short, int, long, float, double, signed, unsigned, _Bool, _Complex, <struct-or-union-specifier>, <enum-specifier>, and <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.

然后继续列出所有多重集,例如 unsigned long 或 unsigned long int.

It then goes on to list all the multisets, such as 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:

unsigned long
long unsigned

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

这篇关于'long unsigned' 和 C 中的 'unsigned long' 一样有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆