473,770 Members | 1,799 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 #1
22 3600
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.
is there any other purpose than this, if so why ...????
No, it's just catering for the lazy.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 16 '08 #2
On Feb 16, 7:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
aark...@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 ..
How do you know? 28.31 is a valid value for initializing in integer
context.
>
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.
Modulus or even the conditional check could be expensive somewhere I
guess.
Notice it's not only allowed in an "initialize r list" but anywhere
where {elements} is used. (I don't know the proper term for it)
For example, printf("%zu\n", sizeof (char[]){1,2,3,});
Feb 16 '08 #3
On Feb 16, 4:55*pm, aark...@gmail.c om wrote:
why does C language permits an extra comma in initializer list
...
*i have heard it is for the purpose of automatic code generation
is there any other purpose than this, if so why ...????
According to K&R2 (p196) it is "a nicety for neat formatting."

--
Martin
Feb 16 '08 #4
vi******@gmail. com said:
On Feb 16, 7:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>aark...@gmail. com 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.

<snip>
>So, as you can see, it isn't actually difficult to generate the code
without the trailing comma.
Modulus or even the conditional check could be expensive somewhere I
guess.
My % could have been avoided if necessary, but in any case it was to deal
with the newline, not the comma.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 16 '08 #5
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
};

I know which one I would prefer to maintain.
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Feb 16 '08 #6
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?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 16 '08 #7
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?
The code that you snipped was not an example of automatically
generated code.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Feb 16 '08 #8
Ben Pfaff said:
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?

The code that you snipped was not an example of automatically
generated code.
Oh. I see.

<bright smile, a la Dory from "Finding Nemo">

Well then - perhaps you ought to automate it! :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 16 '08 #9
Richard Heathfield <rj*@see.sig.in validwrites:
Ben Pfaff said:
>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?

The code that you snipped was not an example of automatically
generated code.

Oh. I see.

<bright smile, a la Dory from "Finding Nemo">

Well then - perhaps you ought to automate it! :-)
Fortunately, I have no need to do so, because trailing commas are
allowed in initializer lists (and elsewhere).
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Feb 16 '08 #10

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

Similar topics

27
4574
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
2821
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
2391
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
3049
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
3264
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
1440
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
2636
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
9617
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
9454
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
10257
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...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
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
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
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
5354
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
2849
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.