Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #29538

Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)

From Tim Rentsch <txr@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)
Date 2012-12-20 10:40 -0800
Organization A noiseless patient Spider
Message-ID <kfnsj70efds.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References (7 earlier) <0.4e0124e757bed0241853.20121216012421GMT.874njmeqm2.fsf@bsb.me.uk> <kajsmp$2a4$1@dont-email.me> <0.f5d24705a97d28e4e8d3.20121216131132GMT.87sj76cfaz.fsf@bsb.me.uk> <kfnr4mpjbws.fsf@x-alumni2.alumni.caltech.edu> <0.930acaaa95a5b6bbc6b9.20121217150832GMT.87mwxcbtsf.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Tim Rentsch <txr@alumni.caltech.edu> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> Shao Miller <sha0.miller@gmail.com> writes:
>>>
>>>> On 12/15/2012 20:24, Ben Bacarisse wrote:
>>>>> Shao Miller <sha0.miller@gmail.com> writes:
>>>>>
>>>>>> On 12/15/2012 17:10, Ben Bacarisse wrote:
>>>>>>> Shao Miller <sha0.miller@gmail.com> writes:
>>>>>>>
>>>>>>>> On 12/15/2012 15:47, Ben Bacarisse wrote:
>>>>>>>>> glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
>>>>>>>>>
>>>>>>>>>> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>>>>>>>>>
>>>>>>>>>> (snip)
>>>>>>>>>>
>>>>>>>>>>> How odd.  What's the point of have both i and i2?  If either gets
>>>>>>>>>>> altered in the "some code" part then there is an argument for not
>>>>>>>>>>> re-writing it as a "for" loop.  Generally, readers will expect the
>>>>>>>>>>> variables mentioned in a "for" loops to be left alone in the body.  This
>>>>>>>>>>> is not rule of the language but it's a good rule of thumb.
>>>>>>>>>>
>>>>>>>>>> I have used "for" loops where I needed "continue" to restart
>>>>>>>>>> the loop without incrementing the index.
>>>>>>>>>>
>>>>>>>>>> Otherwise, I agree.
>>>>>>>>>
>>>>>>>>> So when you want that behaviour you'd write:
>>>>>>>>>
>>>>>>>>>      for (<initialise>; <condition>;) {
>>>>>>>>>        <code that uses continue>
>>>>>>>>>        <increment>
>>>>>>>>>      }
>>>>>>>>>
>>>>>>>>> rather than
>>>>>>>>>
>>>>>>>>>      <initialise>
>>>>>>>>>      while (<condition>) {
>>>>>>>>>        <code that uses continue>
>>>>>>>>>        <increment>
>>>>>>>>>      }
>>>>>>>>>
>>>>>>>>> ?  I don't think I would (and I could make hand-waving arguments about
>>>>>>>>> why I wouldn't) but it's really just a matter to taste.
>>>>>>>>>
>>>>>>>>
>>>>>>>> It can also be a matter of lifetime (for C >= C99), which could
>>>>>>>> potentially impact memory usage.  By using declarations (but not
>>>>>>>> compound literals) in the 'for', such objects needn't've storage for
>>>>>>>> the enclosing block.
>>>>>>>
>>>>>>> That does not match my understanding.  Can you provide supporting
>>>>>>> evidence?
>>>>>>>
>>>>>>> Putting a declaration in a for statement (as opposed to just before a
>>>>>>> while) alters the scope the scope of the name but does not, I think,
>>>>>>> have any effect on the lifetime of the declared object.
>>>>>>
>>>>>> Sure thing.  C99 6.8.5.3p1 with 6.2.4p6.
>>>>>
>>>>> These support exactly what I said.  I don't see any support there for
>>>>> the change in the lifetime that you suggested.
>>>>>
>>>>
>>>> With _much_ apology, I've assumed that N1256.PDF's 6.2.4p6 is C99's.
>>>> I am referring to C99's VLAs, which obviously introduce a number of
>>>> quirks (relative to C89/C90).  One of those quirks is that their
>>>> lifetime and scope are fairly closely tied, and it's relevant for a
>>>> for' loop's clause-1.  (_Why_ one would declare a VLA in a 'for' loop
>>>> is a matter of practicality, but _that_ one can is a matter of fact.)
>>>>
>>>> Or, I could be missing something.
>>>
>>> The "for" and the "while" versions are the same with respect to
>>> lifetime, with or without VLAs.  The loop pattern you commented on had
>>> <initialise> but you decided to make a point about the fact that C99
>>> also permits a declaration there.  That's fine, but your point is still
>>> wrong.  You must then compare
>>>
>>>        for (<declaration>; <condition>;) {
>>>          <code that uses continue>
>>>          <increment>
>>>        }
>>>
>>> with
>>>        <declaration>;
>>>        while (<condition>) {
>>>          <code that uses continue>
>>>          <increment>
>>>        }
>>>
>>> which do not differ significantly in the required lifetime of the
>>> declared object.
>>
>> I believe they do, because of 6.8.5 p5.
>
> Ah!  Thank you.  This not only makes things very clear (in particular my
> fussing over footnote 27 is irrelevant since it clearly applies when a
> for loop exits) it explains what looked to me like inconsistent wording
> between 6.2.1 (Scope of identifiers) p4 and 5.8.5.3 (The for statement)
> p1.
>
> I think it could be better placed, but at least it's there.

Right.  Note that there is similar wording for selection
statements ('if()' and 'switch()').

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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