Extra Comma in enum is Valid?

  • Thread starter Sriram Rajagopalan
  • Start date
S

Sriram Rajagopalan

Hi,

Is the extra comma at the end of an enumerator-list valid according to
the C standards?

With the gcc compiler the following is valid:

enum DAYS {MONDAY, TUESDAY, }day1;

gcc does not even *warn* about the extra comma after "TUESDAY".

I found references which say that this is valid in C99 standard but
invalid in C89. Please help me confirm on this.

Is this compiler specific behavior?

I also include the Backus-Naur form of C syntax for enum:


<type-specifier> ::= void
| char
| short
| int
| long
| float
| double
| signed
| unsigned
| <struct-or-union-specifier>
| <enum-specifier>
| <typedef-name>

<enum-specifier> ::= enum <identifier> { <enumerator-list> }
| enum { <enumerator-list> }
| enum <identifier>

<enumerator-list> ::= <enumerator>
| <enumerator-list> , <enumerator>

<enumerator> ::= <identifier>
| <identifier> = <constant-expression>


The above does grammar does not indicate that there could be a extra
comma at the end of an enumerator-list.

Regards,
Sriram.
 
K

Krishanu Debnath

Sriram said:
Hi,

Is the extra comma at the end of an enumerator-list valid according to
the C standards?

With the gcc compiler the following is valid:

enum DAYS {MONDAY, TUESDAY, }day1;

gcc does not even *warn* about the extra comma after "TUESDAY".

I found references which say that this is valid in C99 standard but
invalid in C89. Please help me confirm on this.

Yes, you are right.

enum-specifier syntax in c89 :
-------------------------------

enum identifieropt { enumerator-list }

enum identifier

enum-specifier syntax in c99 :
--------------------------------

enum identifieropt { enumerator-list }

enum identifieropt { enumerator-list , }

enum identifier
 
M

Michael Mair

Sriram said:
Hi,

Is the extra comma at the end of an enumerator-list valid according to
the C standards?

With the gcc compiler the following is valid:

enum DAYS {MONDAY, TUESDAY, }day1;

gcc does not even *warn* about the extra comma after "TUESDAY".

I found references which say that this is valid in C99 standard but
invalid in C89. Please help me confirm on this.

Is this compiler specific behavior?

I also include the Backus-Naur form of C syntax for enum:


<type-specifier> ::= void
| char
| short
| int
| long
| float
| double
| signed
| unsigned
| <struct-or-union-specifier>
| <enum-specifier>
| <typedef-name>

<enum-specifier> ::= enum <identifier> { <enumerator-list> }
| enum { <enumerator-list> }
| enum <identifier>

<enumerator-list> ::= <enumerator>
| <enumerator-list> , <enumerator>

<enumerator> ::= <identifier>
| <identifier> = <constant-expression>


The above does grammar does not indicate that there could be a extra
comma at the end of an enumerator-list.

It is allowed in C99 but not in C89.
It is also a common compiler extension, as it eases (semi-)automatic
generation of enumerations.
If you use gcc, consider using it in an appropriate mode, i.e.
use the options
-std=c89 -pedantic
or
-std=c99 -pedantic

Without std=cXX, you are in gnu89 mode; without pedantic, the compiler
allows gnu extensions for the respective standard.

Best:
gcc -std=cXX -pedantic -Wall -O

Sources:

C89, last public draft:
"3.5.2.2 Enumeration specifiers

Syntax

enum-specifier:
enum identifier<opt> { enumerator-list }
enum identifier

enumerator-list:
enumerator
enumerator-list , enumerator

enumerator:
enumeration-constant
enumeration-constant = constant-expression
"
C99
"6.7.2.2 Enumeration specifiers
Syntax
1 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
"

Cheers
Michael
 
C

Christian Kandeler

Sriram said:
Is the extra comma at the end of an enumerator-list valid according to
the C standards?

It is valid according to the current C standard.
I found references which say that this is valid in C99 standard but
invalid in C89. Please help me confirm on this.

Correct. The trailing comma is mentioned in the C99 standard as one of the
changes to the previous standard.
[ grammar for enum ]

The above does grammar does not indicate that there could be a extra
comma at the end of an enumerator-list.

So it obviously refers to C89.


Christian
 
M

Martin Ambuhl

Sriram said:
Hi,

Is the extra comma at the end of an enumerator-list valid according to
the C standards?

With the gcc compiler the following is valid:

enum DAYS {MONDAY, TUESDAY, }day1;

gcc does not even *warn* about the extra comma after "TUESDAY".

It does if you use "-std=c89":
" warning: comma at end of enumerator list"
I found references which say that this is valid in C99 standard but
invalid in C89. Please help me confirm on this.

If you want gcc to give c89-specific warnings, use the right compilation
options. The default for gcc gnu99; to get c89, c99, or gnu89 you have
to tell it so. This is getting perilously near being off-topic in clc.
Note that Borland compilers and MS compilers are not ISO-standard by
default either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top