473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extra comma

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 generation
is there any other purpose than this, if so why ...????
Feb 16 '08
22 3601
Richard Heathfield <rj*@see.sig.in validwrites:
Ben Pfaff said:
>Richard Heathfield <rj*@see.sig.in validwrites:

<snip>
>I know which one I would prefer to maintain.

Surely the whole point of generating the code automatically is that you
don't have to maintain it?
I think Ben's point (not that I'd presume to speak for him) is that
the trailing-comma rule can make it easier to maintain the code that
generates the code.

If you want to generate an initializer list and you already have all
the information in an array, then the trailing-comma rule doesn't help
much; it's easy enough to know when you're about to write the last
element and drop the comma. It's *slightly* more convenient to be
able to generate a comma after every item, but not enough to be worth
marring the language.

But if you don't know whether the current item is the last one until
after you've written it (say, you're reading input from a file), then
being able to write the trailing comma after every item can be more
significantly convenient.

It's still not impossible to avoid the trailing comma (if nothing
else, you can build an array or list internally and write the whole
initializer only when it's complete), and I still have mixed feelings
about whether the rule is worthwhile, but it's not completely useless.

An interesting (I think) point is that I've never heard anyone
complain that semicolons are used as terminators rather than as
separators, as they are in Pascal. The fact that we can (and must)
write:
{
statement1;
statement2;
}
rather than
{
statement1;
statement2
}
makes programs easier to maintain when we want to add a third
statement. But the idea that a comma should be a terminator rather
than a separator seems somehow unnatural -- even though they're both
separators in written English.

To summarize:

{ statement1; statement2; } /* good */
{ statement1; statement2 } /* bad */
{ initializer1, initializer2, } /* bad */
{ initializer1, initializer2 } /* good */

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 16 '08 #11
Ben Pfaff wrote:
Richard Heathfield <rj*@see.sig.in validwrites:
>aa*****@gmail.c om said:
>>why does C language permits an extra comma in initializer list
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[i]);
while(i++ < 12)
{
printf(", %d%s", day[i], (i % 6) == 5 ? "\n" : "");
}

Observe:

int array[] = {
#ifdef ELEMENT_ONE
1,
#endif
#ifdef ELEMENT_TWO
2,
#endif
#ifdef ELEMENT_THREE
3,
#endif
#ifdef ELEMENT_FOUR
4,
#endif
};

versus:

int array[] = {
#ifdef ELEMENT_ONE
1
# if (defined(ELEMEN T_TWO) || defined(ELEMENT _THREE) \
|| defined(ELEMENT _FOUR))
,
# endif
#endif
#ifdef ELEMENT_TWO
2,
# if defined(ELEMENT _THREE) || defined(ELEMENT _FOUR)
,
# endif
#endif
#ifdef ELEMENT_THREE
3
# if defined(ELEMENT _FOUR)
,
# endif
#endif
#ifdef ELEMENT_FOUR
4,
#endif
};
Ben meant to omit the comma following "4", of course. Perhaps the mistake
helps illustrate his point.

Richard Heathfield wrote:
Ben Pfaff said:
>I know which one I would prefer to maintain.

Surely the whole point of generating the code automatically is that you
don't have to maintain it?
Ben's example was for code that is manually written and maintained, not
automatically generated. I think his point has merit.

--
Thad
Feb 16 '08 #12

"Keith Thompson" <ks***@mib.orgw rote in message
An interesting (I think) point is that I've never heard anyone
complain that semicolons are used as terminators rather than as
separators, as they are in Pascal. The fact that we can (and must)
write:
{
statement1;
statement2;
}
rather than
{
statement1;
statement2
}
makes programs easier to maintain when we want to add a third
statement. But the idea that a comma should be a terminator rather
than a separator seems somehow unnatural -- even though they're both
separators in written English.
Most people don't know the rules for semicolons in written English. The
semi-colon can be used to introduce a list, or to separate two parts of a
sentence which are themselves valid sentences (periods).

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 16 '08 #13
"Malcolm McLean" <re*******@btin ternet.comwrite s:
Most people don't know the rules for semicolons in written
English. The semi-colon can be used to introduce a list, or to
separate two parts of a sentence which are themselves valid sentences
(periods).
Ordinarily, a colon, not a semicolon, would be used in English to
introduce a list.
--
Ben Pfaff
http://benpfaff.org
Feb 16 '08 #14

"Ben Pfaff" <bl*@cs.stanfor d.eduwrote in message
news:87******** ****@blp.benpfa ff.org...
"Malcolm McLean" <re*******@btin ternet.comwrite s:
>Most people don't know the rules for semicolons in written
English. The semi-colon can be used to introduce a list, or to
separate two parts of a sentence which are themselves valid sentences
(periods).

Ordinarily, a colon, not a semicolon, would be used in English to
introduce a list.
I mean terminate a comma-separated list.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 16 '08 #15
aa*****@gmail.c om 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
Feb 17 '08 #16
SM Ryan wrote:
aa*****@gmail.c om 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.

--
Ian Collins.
Feb 17 '08 #17
On Sun, 17 Feb 2008 14:18:51 +1300, Ian Collins <ia******@hotma il.com>
wrote in comp.lang.c:
SM Ryan wrote:
aa*****@gmail.c om 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.
Legalizing the trailing comma in an enumeration declaration was new in
C99. The optional trailing comma was left out of the ANSI 89/ISO 90
grammar by mistake.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 17 '08 #18
Richard Heathfield wrote:
aa*****@gmail.c om 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[i]);
while(i++ < 12)
{
printf(", %d%s", day[i], (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
}

--
Army1987 (Replace "NOSPAM" with "email")
Feb 17 '08 #19
On Sun, 17 Feb 2008 11:15:44 +0000, Army1987 wrote:
Richard Heathfield wrote:
>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.
Feb 17 '08 #20

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

Similar topics

27
4575
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
4
2822
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous page, puts that into an array and inserts the array into SQL server. Now here is the problem:
11
2392
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal state\n"), TRUE) : \ (state = newstate, FALSE) This macro is called like this: setState(state, ST_Used);
21
3051
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as opposed to what is mentioned in precedence table? Also, with comma operator. consider,
5
4530
by: Sriram Rajagopalan | last post by:
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".
9
4515
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the city state. Is there a built in function that I'm missing that will do the split and recognize that the comma in city state is part of the item? --
1
3265
by: Benjamin Rutt | last post by:
There has been a problem that has been bugging me for a while for reading input from standard in. Consider the following simple program: #!/usr/bin/env python import sys print 'enter something: ', answer = sys.stdin.readline().strip() print 'you answered {%s}' % (answer) When I run this interactively, the following happens:
4
1441
by: alf | last post by:
Hi, I can not find out where the extra space comes from. Run following: import os,sys while 1: print 'Question ]?', if sys.stdin.readline().strip() in ('Y','y'): #do something pass
15
2637
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of sizeof." I think this rule is easy to understand. Because I can find the contexts of applying the rule as follows. (1) int* p = 0; int b1 = sizeof(*p); // OK, b1 = 4, *p would not be evaluated.
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.