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;
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.
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.
This can be likened to the oft-seen SQL:
select fld1, fld2 from tbl where 1=1 and fld1 > 8
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 :-)
Any decent DBMS query optimiser should be able to strip out constant clause like that before going to the database tables.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…