extra comma

S

SM Ryan

(e-mail address removed) wrote:
# Hi all,
#
# why does C language permits an extra comma in initializer list

Keypunching.

You don't want to modify an existing card deck just because you
insert a new card in the deck. It's the same reason for the semicolon
rules.

If you want to understand this better, next time to edit a source
file, restrict your actions to only
Deleting an entire line.
Inserting an entire line.
Moving existing lines.
Retyping an entire line.
 
I

Ian Collins

SM said:
(e-mail address removed) wrote:
# Hi all,
#
# why does C language permits an extra comma in initializer list

Keypunching.

You don't want to modify an existing card deck just because you
insert a new card in the deck. It's the same reason for the semicolon
rules.
I thought legalising the trailing comma was new in C99. Not many card
readers where in use in 1999.
 
K

karthikbalaguru

rzed said:



Right. And keyboards are lame, too - we should be using plugboards.


True enough. In fact, that's catering for the good kind of lazy (yes, there
are at least two kinds). Of course, manually reordering lists is easy
enough even if you don't like the trailing comma. Reorder, remove last
comma, recompile, and the editor will tell you where to insert the comma.
:)


I certainly wouldn't want to see the feature /removed/ from the language.
Had I been consulted as to its original addition, I'd probably have argued
against it, but hey - it's there, it works, it does no particular harm, so
no big deal. (Of course, removing it from the Standard would do no harm
either, since the standardisation process is effectively dead in the water
now.)

These extra commas are permitted but are not required.
A benefit is in manual re-oredering. :):)

Karthik Balaguru
 
J

Jack Klein

A

Army1987

Richard said:
(e-mail address removed) said:
Hi all,

why does C language permits an extra comma in initializer list

ex:- int days[] = {
31,28.31,30,31,30,

Between 31 and 28 you meant ,, not ..
31,31,30,31,30,31,
}
i have heard it is for the purpose of automatic code generation

That's supposed to be the reasoning behind such lamenesses, yes. But
observe:

i = 0;
printf(" %d", day);
while(i++ < 12)
{
printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
}

So, as you can see, it isn't actually difficult to generate the code
without the trailing comma.


int foo[] = {
#ifdef FOO
42, 0,
#endif
37, EOF,
#ifdef BAR
'\n', EXIT_FAILURE,
#endif
#ifdef BAZ
SEEK_END, ERANGE,
#endif
}
 
H

Harald van Dijk

Richard said:
So, as you can see, it isn't actually difficult to generate the code
without the trailing comma.

int foo[] = {
#ifdef FOO
42, 0,
#endif
37, EOF,
#ifdef BAR
'\n', EXIT_FAILURE,
#endif
#ifdef BAZ
SEEK_END, ERANGE,
#endif
}

int foo[] = {
#ifdef FOO
42, 0,
#endif
37, EOF
#ifdef BAR
,'\n', EXIT_FAILURE
#endif
#ifdef BAZ
,SEEK_END, ERANGE
#endif
}

It's not difficult when you have at least one fixed item in the list.
 
W

William Pursell

Observe:

int array[] = {
#ifdef ELEMENT_ONE
1,
#endif
};

versus:

int array[] = {
#ifdef ELEMENT_ONE
1
# if (defined(ELEMENT_TWO) || defined(ELEMENT_THREE) \
|| defined(ELEMENT_FOUR))
,
# endif
#endif
};

I know which one I would prefer to maintain.

I can't think of too many cases where it is problematic to do:
int array[] = {
#ifdef ELEMENT_ONE
1,
#endif
SENTINEL
};

to force the existence of at least one fixed
element.
 
A

Army1987

Harald said:
int foo[] = {
#ifdef FOO
42, 0,
#endif
37, EOF
#ifdef BAR
,'\n', EXIT_FAILURE
#endif
#ifdef BAZ
,SEEK_END, ERANGE
#endif
}

It's not difficult when you have at least one fixed item in the list.
But it is so ugly! :)
 
A

aarklon

(e-mail address removed) said:
(e-mail address removed) said:
Hi all,
why does C language permits an extra comma in initializer list
ex:- int days[] = {
31,28.31,30,31,30,
Between 31 and 28 you meant ,, not ..
How do you know?

Call me psychic if you like.
28.31 is a valid value for initializing in integer
context.

Yes, but on this occasion it was not what was intended.

Yeah richard is correct he is indeed psychic to a certain extent
I did n't mean 28.31 , i meant 28,31
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top