473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Extra Comma in enum is Valid?

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.

Nov 15 '05 #1
5 4496

Sriram Rajagopalan wrote:
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
--
kr******@india.ti.com

Nov 15 '05 #2
Sriram Rajagopalan wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
Sriram Rajagopalan wrote:
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
Nov 15 '05 #4
Thanks for all of your comments. The concept is clear now.

-Sriram.

Nov 15 '05 #5
Sriram Rajagopalan wrote:
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.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
12
by: Serve Laurijssen | last post by:
Is code like the following allowed? I am talking about the comma after the last function in the initializer. void f(void) {puts("f");} void g(void) {puts("g");} struct Funcs { void...
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
22
by: aarklon | last post by:
Hi all, why does C language permits an extra comma in initializer list ex:- int days = { 31,28.31,30,31,30, 31,31,30,31,30,31, } i have heard it is for the purpose of automatic code...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.