Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)

Reply
Thread Tools

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

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-17-2012
Tim Rentsch <(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> writes:
>
>> Shao Miller <(E-Mail Removed)> writes:
>>
>>> On 12/15/2012 20:24, Ben Bacarisse wrote:
>>>> Shao Miller <(E-Mail Removed)> writes:
>>>>
>>>>> On 12/15/2012 17:10, Ben Bacarisse wrote:
>>>>>> Shao Miller <(E-Mail Removed)> writes:
>>>>>>
>>>>>>> On 12/15/2012 15:47, Ben Bacarisse wrote:
>>>>>>>> glen herrmannsfeldt <(E-Mail Removed)> writes:
>>>>>>>>
>>>>>>>>> Ben Bacarisse <(E-Mail Removed)> 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.

--
Ben.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-17-2012
Tim Rentsch <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> writes:

[...]
>> 6.8.5.3p1:
>> If *clause-1* is a declaration, the scope of any identifiers
>> it declares is the remainder of the declaration and the entire
>> loop, including the other two expressions
>>
>> 6.2.4p6:
>> For such an object that does not have a variable length array type,
>> its lifetime extends from entry into the block with which it is
>> associated until execution of that block ends in any way.
>>
>> So my interpretation of that is:
>>
>> void foo(void) { /* lifetime of i starts here */
>> this();
>> that();
>> for (int i = 0 /* scope of i starts here */; i < 100; i ++) {
>> the_other_thing(i);
>> /* scope of i ends here */
>> }
>> yet_another_thing();
>> /* lifetime of i ends here */
>> }
>>
>> The "block with which it [i] is associated" is the outer block for the
>> function, not the block that is the statement controlled by the for
>> loop.

>
> Actually it's the block of the iteration ('for') statement.
>
>> Remember that the statement controlled by a for loop needn't be a block:
>>
>> for (int i = 0; i < 100; i ++)
>> single_statement();

>
> The loop body of an iteration statement is always a block, even if
> just a single statement (ie, not a compound statement).


You're right. I've been assuming that "block" and "compound-statement"
are synonymous, or nearly so, but N1570 6.8.5p5 says:

An iteration statement is a block whose scope is a strict
subset of the scope of its enclosing block. The loop body is
also a block whose scope is a strict subset of the scope of
the iteration statement.

(It would have been nice if that had been mentioned in the definition
of the word "block" in 6.8p3, but that so-called "definition"
describes how blocks behave, not what they are.)

>> If `i` were defined just above the for loop, the lifetime and scope would
>> be the same as they are in my example.

>
> Declaring in the 'for' statement, as opposed to just before, is
> more limited in both scope and lifetime. All of these follow from
> 6.8.5 p5.


Yup.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-17-2012
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:

[...]
>> The "block with which it [i] is associated" is the outer block for the
>> function, not the block that is the statement controlled by the for
>> loop.

>
> No.


You're right.

> If you're going to say that there is one block
> with which (i) is associated,
> then it is the iteration statement itself,
> which is the block in which (i) is declared.
>
> N1570
> 6.8.5 Iteration statements
> 5 An iteration statement is a block ...
>
> In C90 "block" was synonymous with "compound statement".
> In C99, that was changed to the way that C11 has it now.


Yes, I missed that.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      12-17-2012
Phil Carmody <(E-Mail Removed)> writes:
> This reinforces something that I've spotted historically - there seems to
> be some strange fetish for using the swiss-army for(; where while() is
> perfectly suitable.


Perhaps I should have worded those as just "for" and "while", I wasn't
trying to imply the "(;" part, just the iteration statements.

--
I'm not saying that google groups censors my posts, but there's a strong link
between me saying "google groups sucks" in articles, and them disappearing.

Oh - I guess I might be saying that google groups censors my posts.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-18-2012
pete <(E-Mail Removed)> writes:

> Phil Carmody wrote:
>> Phil Carmody <(E-Mail Removed)> writes:
>> > This reinforces something that I've spotted historically
>> > - there seems to be some strange fetish
>> > for using the swiss-army for(;
>> > where while() is perfectly suitable.

>>
>> Perhaps I should have worded those as just "for" and "while", I wasn't
>> trying to imply the "(;" part, just the iteration statements.

>
> for (; is special.
>
> while () , doesn't compile,
> even though K&R 3.5 says that they are equivalent.


That's a very odd reading of the text! I've tried a few times and I
can't get that meaning from what's written in my K&R.

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-18-2012
pete <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>>
>> pete <(E-Mail Removed)> writes:
>>
>> > Phil Carmody wrote:
>> >> Phil Carmody <(E-Mail Removed)> writes:
>> >> > This reinforces something that I've spotted historically
>> >> > - there seems to be some strange fetish
>> >> > for using the swiss-army for(;
>> >> > where while() is perfectly suitable.
>> >>
>> >> Perhaps I should have worded those as just
>> >> "for" and "while", I wasn't
>> >> trying to imply the "(;" part, just the iteration statements.
>> >
>> > for (; is special.
>> >
>> > while () , doesn't compile,
>> > even though K&R 3.5 says that they are equivalent.

>>
>> That's a very odd reading of the text! I've tried a few times and I
>> can't get that meaning from what's written in my K&R.

>
> The for statement
>
> for (expr1; expr2; expr3)
> statement
>
> is equivalent to
>
> expr1;
> while (expr2) {
> statement
> expr3;
> }
>
> except for the behavior of continue, which is described in 3.7.


Does it not go on to explain what happens when any/all of expr[123] are
missing? Mine does.

Even if you stopped there and chose to ignore the sentences that follow,
I don't see how you can conclude that the equivalence applies to "for
(;". You'd have to be sure that expr[123] can be "empty" and that's
not at all clear. We've been told about expressions in chapter 2, but
none of the have been "empty". We are told, right away, that it's
possible to omit these parts of the for loop, but the special meaning is
explained then and there in the sentences that explain that they can be
omitted.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-18-2012
On 12/17/2012 08:53 PM, pete wrote:
> Ben Bacarisse wrote:
>>
>> pete <(E-Mail Removed)> writes:
>>
>>> Phil Carmody wrote:
>>>> Phil Carmody <(E-Mail Removed)> writes:
>>>>> This reinforces something that I've spotted historically
>>>>> - there seems to be some strange fetish
>>>>> for using the swiss-army for(;
>>>>> where while() is perfectly suitable.
>>>>
>>>> Perhaps I should have worded those as just
>>>> "for" and "while", I wasn't
>>>> trying to imply the "(;" part, just the iteration statements.
>>>
>>> for (; is special.
>>>
>>> while () , doesn't compile,
>>> even though K&R 3.5 says that they are equivalent.

>>
>> That's a very odd reading of the text! I've tried a few times and I
>> can't get that meaning from what's written in my K&R.

>
> The for statement
>
> for (expr1; expr2; expr3)
> statement
>
> is equivalent to
>
> expr1;
> while (expr2) {
> statement
> expr3;
> }
>
> except for the behavior of continue, which is described in 3.7.


There's a few other differences, the relevant one in this context being
that expr2 is optional if a for() statement, but mandatory in a while()
(6.8.5p1).
Also, what you've called expr1 is called clause-1 in the standard, to
reflect the fact that it can be either an expression or a declaration.
If it is a declaration, any identifiers it declares have a scope that
includes the entire for() statement, but does not extend to the
enclosing block, so you should enclose your 'while' replacement in {},
to give those identifiers the right scope.
--
James Kuyper
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-18-2012
pete <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>>
>> pete <(E-Mail Removed)> writes:
>>
>> > Ben Bacarisse wrote:
>> >>
>> >> pete <(E-Mail Removed)> writes:
>> >>
>> >> > Phil Carmody wrote:
>> >> >> Phil Carmody <(E-Mail Removed)> writes:
>> >> >> > This reinforces something that I've spotted historically
>> >> >> > - there seems to be some strange fetish
>> >> >> > for using the swiss-army for(;
>> >> >> > where while() is perfectly suitable.
>> >> >>
>> >> >> Perhaps I should have worded those as just
>> >> >> "for" and "while", I wasn't
>> >> >> trying to imply the "(;" part, just the iteration statements.
>> >> >
>> >> > for (; is special.
>> >> >
>> >> > while () , doesn't compile,
>> >> > even though K&R 3.5 says that they are equivalent.
>> >>
>> >> That's a very odd reading of the text! I've tried a few times and I
>> >> can't get that meaning from what's written in my K&R.
>> >
>> > The for statement
>> >
>> > for (expr1; expr2; expr3)
>> > statement
>> >
>> > is equivalent to
>> >
>> > expr1;
>> > while (expr2) {
>> > statement
>> > expr3;
>> > }
>> >
>> > except for the behavior of continue, which is described in 3.7.

>>
>> Does it not go on to explain what happens when any/all of expr[123] are
>> missing? Mine does.
>>
>> Even if you stopped there and chose to ignore the sentences that follow,
>> I don't see how you can conclude that the equivalence applies to "for
>> (;". You'd have to be sure that expr[123] can be "empty" and that's
>> not at all clear. We've been told about expressions in chapter 2, but
>> none of the have been "empty". We are told, right away, that it's
>> possible to omit these parts of the for loop, but the special meaning is
>> explained then and there in the sentences that explain that they can be
>> omitted.

>
> If 3.5 had said that Superman and his sister
> were equivalent except that Superman was a male, as described in 3.7.
> and then the text said that Superman had x-ray vision,
> I would take that to mean that Supergirl had x-ray vision too.


Which is not analogous to what K&R wrote. It does not say that all
forms of "for" can be mapped to the given "while". It says that some
forms (the ones that have three nonempty expressions) can be so mapped.
It then confirms that this is not a universal rule by detailing special
cases and how they affect the correspondence.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-18-2012
pete <(E-Mail Removed)> writes:
> Ben Bacarisse wrote:
>> pete <(E-Mail Removed)> writes:
>> > Ben Bacarisse wrote:
>> >> pete <(E-Mail Removed)> writes:

[...]
>> >> > for (; is special.
>> >> >
>> >> > while () , doesn't compile,
>> >> > even though K&R 3.5 says that they are equivalent.
>> >>
>> >> That's a very odd reading of the text! I've tried a few times and I
>> >> can't get that meaning from what's written in my K&R.
>> >
>> > The for statement
>> >
>> > for (expr1; expr2; expr3)
>> > statement
>> >
>> > is equivalent to
>> >
>> > expr1;
>> > while (expr2) {
>> > statement
>> > expr3;
>> > }
>> >
>> > except for the behavior of continue, which is described in 3.7.

>>
>> Does it not go on to explain what happens when any/all of expr[123] are
>> missing? Mine does.
>>
>> Even if you stopped there and chose to ignore the sentences that follow,
>> I don't see how you can conclude that the equivalence applies to "for
>> (;". You'd have to be sure that expr[123] can be "empty" and that's
>> not at all clear. We've been told about expressions in chapter 2, but
>> none of the have been "empty". We are told, right away, that it's
>> possible to omit these parts of the for loop, but the special meaning is
>> explained then and there in the sentences that explain that they can be
>> omitted.

>
> If 3.5 had said that Superman and his sister
> were equivalent except that Superman was a male, as described in 3.7.
> and then the text said that Superman had x-ray vision,
> I would take that to mean that Supergirl had x-ray vision too.


The text doesn't say that for loops *in general* are equivalent to
the given while loop. It says that

for (expr1; expr2; expr3)
statement

is equivalent to the given while loop. It refers to a *subset*
of all for loops, i.e., those for which all three exprs are present.

It goes on to say that any or all of expr1, expr2, and expr3 may
be *omitted*, not that any of them may be *empty*. (And the names
imply that they're expressions; there's no such thing as an empty
expression.) If expr1 or expr3 is omitted, it's simply dropped
from the expansion. If expr2 is omitted, K&R doesn't even show an
equivalent while loop; it merely says that the for loop is infinite.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      12-20-2012
pete <(E-Mail Removed)> writes:
> Phil Carmody wrote:
> > Phil Carmody <(E-Mail Removed)> writes:
> > > This reinforces something that I've spotted historically
> > > - there seems to be some strange fetish
> > > for using the swiss-army for(;
> > > where while() is perfectly suitable.

> >
> > Perhaps I should have worded those as just "for" and "while", I wasn't
> > trying to imply the "(;" part, just the iteration statements.

>
> for (; is special.
>
> while () , doesn't compile,
> even though K&R 3.5 says that they are equivalent.


Now read what I just wrote, and pay attention to where I say you should ignore the
open bracket, the two semicolons, and the closed bracket.

Phil
--
I'm not saying that google groups censors my posts, but there's a strong link
between me saying "google groups sucks" in articles, and them disappearing.

Oh - I guess I might be saying that google groups censors my posts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to format $mail_from with 2 variables separated by a comma? Jason Miles Perl 1 05-28-2004 02:48 AM
Decimal point is replaced with comma Rasool ASP .Net 3 03-03-2004 06:02 PM
Comma Delimited Yama ASP .Net 1 12-16-2003 03:46 AM
server.mappath with comma in foldername? Martin Pahlplatz ASP .Net 0 11-11-2003 10:08 PM
add a comma Ryan Moore ASP .Net 4 08-28-2003 03:32 PM



Advertisments