是否需要用C枚举最后一个逗号? [英] Is the last comma in C enum required?

查看:2221
本文介绍了是否需要用C枚举最后一个逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是在C枚举声明所需的最后一个逗号?

Is the last comma required in a C enum declaration?

即。在VAL3逗号后需要?

i.e. is the comma after VAL3 required?

enum{Val1, Val2, Val3,} someEnum;

是否有/离开它的任何副作用

Are there any side-effects of leaving it in/out

感谢

推荐答案

它不是必需的。第 6.7.2.2 C99的列出了语法为:

It's not required. Section 6.7.2.2 of C99 lists the syntax as:

enum-specifier:
    enum identifieropt { enumerator-list }
    enum identifieropt { enumerator-list , }
    enum identifier
enumerator-list:
    enumerator
    enumerator-list , enumerator
enumerator:
    enumeration-constant
    enumeration-constant = constant-expression

注意,前两种形式枚举符,一个具有后面的逗号,一个没有。的

Notice the first two forms of enum-specifier, one with the trailing comma and one without.

一个优点我已经看到了使用它是这样的话:

One advantage I've seen to using it is in things like:

enum {
    Val1,
    Val2,
    Val3,
} someEnum;

在这里,如果你想添加的(例如) VAL4 Val5 ,你只要复制和粘贴而不必担心调整逗号 VAL3 行。

where, if you want to add in (for example) Val4 and Val5, you just copy and paste the Val3 line without having to worry about adjusting commas.

和,如在注释中指出,它也可以是,以简化自动的code发电机使得它们不必具有特殊处理的最终值。他们可以输出每一个值,后跟一个逗号。

And, as pointed out in a comment, it can also be to simplify automated code generators so that they don't have to have special handling for the final value. They can just output every value followed by a comma.

这可以比喻为人们经常看到SQL:

This can be likened to the oft-seen SQL:

select fld1, fld2 from tbl where 1=1 and fld1 > 8

在这种情况下, 1 = 1 仅在那里,这样你就不必把其中,在你的第一个子句和以后每次一前。你可以只依靠一个事实,即其中,已经存在,只是使用为您添加的所有的人。

In that case, the where 1=1 is there only so that you don't have to put a where before your first clause and an and before each subsequent one. You can just rely on the fact that the where is already there and just use and for all the ones you add.

有些人可能会认为这弥漫着懒惰,他们是对的,但是这并不一定是坏事: - )

Some people may think this reeks of laziness and they're right, but that's not necessarily a bad thing :-)

任何像样的DBMS查询优化应能才去数据库表中剥离出这样的常量条款。

Any decent DBMS query optimiser should be able to strip out constant clause like that before going to the database tables.

这篇关于是否需要用C枚举最后一个逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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