Is the last comma required in a C enum declaration?
That is, is the comma after Val3 required?
enum { Val1, Val2, Val3, } someEnum;
Are there any side-effects of leaving it in/out?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s not required. Section
6.7.2.2of C99 lists the syntax as: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:
where, if you want to add in (for example)
Val4andVal5, you just copy and paste theVal3line 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:
In that case, the
where 1=1is there only so that you don’t have to put awherebefore your first clause and anandbefore each subsequent one. You can just rely on the fact that thewhereis already there and just useandfor 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.