Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) |
| Date | 2012-12-15 09:24 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <kai17o$sph$1@dont-email.me> (permalink) |
| References | <rfvoc8ludt8psl9s51v9re04gvuqhmj36v@4ax.com> |
On 12/15/2012 08:47 AM, Test wrote:
> I have:
>
> for (i=0,i2=0;i<10;i++,i2++)
> {
> .. some code
> }
>
> Is "," (comma) oke here?
Yes.
> I am converting below code to for -loop:
>
> i=0;
> i2=0;
> while (i<10)
> {
> ... some code
> i++;
> i2++;
> }
>
> Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?
The expression i=0 is executed first, though in this particular case it
doesn't matter.
> Any caveats? I could not find explanation to comma usage (maybe not not looking
> hard enough).
The expression E1,E2, where E1 is any expression, and E2 is any
expression other than another comma expression, causes expression E1 to
be evaluated. The result of that evaluation is discarded, so it only
make sense to do this if E1 is an expression with side effects, such as
a=3, b++, or printf("error:%g", d). After all side effects of E1 are
complete, expression E2 is evaluated. The value of expression E2 is also
the value of the comma expression as a whole. Thus "a = (b++,c)" is
equivalent to "b++; a=c;". It only makes sense to use a comma expression
when such a re-write is not possible, as in a for() statement such as
the one you ask about above.
A key point to understand is that commas are NOT always part of a comma
expression. The parameters of a function declaration and the arguments
of a function call are separated by commas, and the same is true for
function-like macros. Commas separate the initializers for the elements
of an array or members of a structure. A single declaration can declare
multiple declarators, separated by commas, as in "int a,b;". Commas are
used to separate the members of a union or enumeration. Commas are also
used in _Generic() expressions and _Static_assert() declarations, which
are new features of C2011, syntactically similar to function calls.
If you wish to use a comma expression in one of those contexts, you'll
have to parenthesize it, to prevent misinterpretation of the comma.
printf("hello %s", "world!\n") does something different from
printf( ("hello %s", "world!\n") ).
--
James Kuyper
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Test <test@.nil.invalid.com> - 2012-12-15 15:47 +0200
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 14:05 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 20:26 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 20:47 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Les Cargill <lcargill99@comcast.com> - 2012-12-15 15:34 -0600
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-12-16 03:23 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-15 16:37 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 22:10 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-15 17:32 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-15 15:43 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 02:15 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-16 14:21 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 18:34 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 01:15 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-17 10:30 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 16:07 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 08:13 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-17 10:33 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 01:24 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 02:19 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 13:11 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 14:51 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 21:16 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 00:55 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-17 15:08 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 10:40 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 22:09 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-15 06:11 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Les Cargill <lcargill99@comcast.com> - 2012-12-15 13:26 -0600
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-15 09:24 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-15 12:21 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-15 16:42 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Philip Lantz <prl@canterey.us> - 2012-12-16 00:09 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-16 08:46 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-16 14:29 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-16 10:30 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-17 12:42 +0200
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-17 23:07 +0200
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 19:31 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 01:32 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 20:53 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 02:29 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-18 05:48 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 11:26 +0000
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-20 16:20 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-18 03:44 -0800
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-17 22:13 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-18 05:40 -0500
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-20 15:14 +0200
Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-20 16:31 -0500
csiph-web