Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Factorials were someone's attempt to make math LOOK exciting.


devel / comp.lang.c / Re: some observation about declaration in loop

SubjectAuthor
* some observation about declaration in loopfir
+* Re: some observation about declaration in loopBen
|`* Re: some observation about declaration in loopfir
| +- Re: some observation about declaration in loopAndrey Tarasevich
| +* Re: some observation about declaration in loopArnold Ziffel
| |`* Re: some observation about declaration in loopBen
| | `* Re: some observation about declaration in loopfir
| |  `* Re: some observation about declaration in loopRichard Damon
| |   +* Re: some observation about declaration in loopBarry Schwarz
| |   |+* Re: some observation about declaration in loopSams Lara
| |   ||`- Re: some observation about declaration in loopBarry Schwarz
| |   |+* Re: some observation about declaration in loopKeith Thompson
| |   ||`* Re: some observation about declaration in loopRichard Damon
| |   || `* Re: some observation about declaration in loopMichael Bäuerle
| |   ||  `* Re: some observation about declaration in loopKeith Thompson
| |   ||   `- Re: some observation about declaration in loopMichael Bäuerle
| |   |`* Re: some observation about declaration in loopfir
| |   | `- Re: some observation about declaration in loopfir
| |   `- Re: some observation about declaration in loopAndrey Tarasevich
| +* Re: some observation about declaration in loopBen
| |`- Re: some observation about declaration in loopfir
| `* Re: some observation about declaration in loopLori Fairhead
|  `* Re: some observation about declaration in loopKeith Thompson
|   `* Re: some observation about declaration in loopLori Fairhead
|    `- Re: some observation about declaration in loopKeith Thompson
+* Re: some observation about declaration in loopAndrey Tarasevich
|`* Re: some observation about declaration in loopfir
| `* Re: some observation about declaration in loopfir
|  +- Re: some observation about declaration in loopAndrey Tarasevich
|  `* Re: some observation about declaration in loopArnold Ziffel
|   +* Re: some observation about declaration in loopfir
|   |`* Re: some observation about declaration in loopfir
|   | `- Re: some observation about declaration in loopArnold Ziffel
|   `- Re: some observation about declaration in loopJack Lemmon
+* Re: some observation about declaration in loopChristian Hanné
|`- Re: some observation about declaration in loopfir
+* Re: some observation about declaration in loopbart c
|`- Re: some observation about declaration in loopfir
`- Re: some observation about declaration in loopJohn Bode

Pages:12
Re: some observation about declaration in loop

<87ee1ncu5f.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21423&group=comp.lang.c#21423

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sat, 23 Apr 2022 15:27:40 -0700
Organization: None to speak of
Lines: 19
Message-ID: <87ee1ncu5f.fsf@nosuchdomain.example.com>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<97afe458-a302-4cd0-89dc-e6416daf2948n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="de37a51cdfcc9bf522499568c2f037fc";
logging-data="9298"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0UCalOc3017sCKQHVYIeb"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:e5brfruIFanDYD6RONIb5MeVcVc=
sha1:e5XCJIx4uM7mgPdPiv6vNRnL7IM=
 by: Keith Thompson - Sat, 23 Apr 2022 22:27 UTC

Lori Fairhead <lorif32768@gmail.com> writes:
[...]
> Just think of it as FOR parameters DO command
>
> Each time the paramters tell FOR to execute the command
>
> In this case the command is a BLOCK so the block is executed
> again and again for each iteration of the FOR loop.
>
> You can have a BLOCK anywhere you have a command like several other
> languages.

It's helpful to use the standard terminology. C has *statements*, not
*commands*.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: some observation about declaration in loop

<87a6cbctr5.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21424&group=comp.lang.c#21424

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sat, 23 Apr 2022 15:36:14 -0700
Organization: None to speak of
Lines: 52
Message-ID: <87a6cbctr5.fsf@nosuchdomain.example.com>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk>
<332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad>
<sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="de37a51cdfcc9bf522499568c2f037fc";
logging-data="9298"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lueOZL5Kyx0Np5/XEyJfj"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:31M8cZU1V6c08ErAD/2TOqxiZFA=
sha1:DDBOcMoY6izXN1zVM7IL7auLOQs=
 by: Keith Thompson - Sat, 23 Apr 2022 22:36 UTC

Barry Schwarz <schwarzb@delq.com> writes:
> On Sat, 23 Apr 2022 13:26:56 -0400, Richard Damon
> <Richard@Damon-Family.org> wrote:
>
>>The key difference between
>>
>>int x = 10;
>>
>>and
>>
>>int x;
>>x = 10;
>>
>>is that the first INITIALIZES x when it is created, and the second
>>ASSIGNS it when the statement is executed.
>>
>>When we change that to
>>
>>static int x = 10;
>>
>>Then by the rules of the C language, that initialization will only
>>happen the first time that we reach that statement, as after that the
>>variable has already been created, so it isn't initiialized again.
>>
>>In fact, behind the scenes, there needs to be some other hidden flag
>>created by the implementation to know if initialization has happened.
>
> I disagree. Paragraph 5.1.2 of C11 explicitly states that a static
> variable is initialized prior to program start up.

Right -- and that's possible because the initializer for a static object
has to be constant.

> The key difference between "int x" (or "int x=10") and "static int x"
> (with or without explicit initialization) is that during execution the
> former causes the variable to be created (and initialized if
> specified) while the latter does absolutely nothing since the variable
> has already been created and initialized.
>
> A hidden flag is not needed at all.

Right.

<OT> C++ allows static objects to have non-constant initialization, so
it does have to ensure that the initialization happens only the first
time the declaration is encountered. A hidden flag is the obvious way
to implement that behavior. </OT>

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: some observation about declaration in loop

<t41v6t$duf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21425&group=comp.lang.c#21425

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sat, 23 Apr 2022 15:39:24 -0700
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <t41v6t$duf$1@dont-email.me>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk>
<332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Apr 2022 22:39:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="826633137e0ac2cebe0f7974e3fec412";
logging-data="14287"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4eRQSfszRzfci3zE/Uq7e"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:ZesS/2JnPdP6U8feZPfB+2nSDeM=
In-Reply-To: <BFW8K.630276$LN2.557300@fx13.iad>
Content-Language: en-US
 by: Andrey Tarasevich - Sat, 23 Apr 2022 22:39 UTC

On 4/23/2022 10:26 AM, Richard Damon wrote:
>
> When we change that to
>
> static int x = 10;
>
> Then by the rules of the C language, that initialization will only
> happen the first time that we reach that statement, as after that the
> variable has already been created, so it isn't initiialized again.

No. This sounds like a rule from C++ language. C is not C++.

In C language static initialization is always done "prior to program
startup" (as the Standard says in 6.2.4/3).

This is, BTW, why static initializers in C are required to be constant
expressions.

--
Best regards,
Andrey Tarasevich

Re: some observation about declaration in loop

<kY%8K.632211$7F2.240543@fx12.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21426&group=comp.lang.c#21426

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.8.1
Subject: Re: some observation about declaration in loop
Content-Language: en-US
Newsgroups: comp.lang.c
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk>
<332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad>
<sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
<87a6cbctr5.fsf@nosuchdomain.example.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <87a6cbctr5.fsf@nosuchdomain.example.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 61
Message-ID: <kY%8K.632211$7F2.240543@fx12.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sat, 23 Apr 2022 19:28:16 -0400
X-Received-Bytes: 3263
 by: Richard Damon - Sat, 23 Apr 2022 23:28 UTC

On 4/23/22 6:36 PM, Keith Thompson wrote:
> Barry Schwarz <schwarzb@delq.com> writes:
>> On Sat, 23 Apr 2022 13:26:56 -0400, Richard Damon
>> <Richard@Damon-Family.org> wrote:
>>
>>> The key difference between
>>>
>>> int x = 10;
>>>
>>> and
>>>
>>> int x;
>>> x = 10;
>>>
>>> is that the first INITIALIZES x when it is created, and the second
>>> ASSIGNS it when the statement is executed.
>>>
>>> When we change that to
>>>
>>> static int x = 10;
>>>
>>> Then by the rules of the C language, that initialization will only
>>> happen the first time that we reach that statement, as after that the
>>> variable has already been created, so it isn't initiialized again.
>>>
>>> In fact, behind the scenes, there needs to be some other hidden flag
>>> created by the implementation to know if initialization has happened.
>>
>> I disagree. Paragraph 5.1.2 of C11 explicitly states that a static
>> variable is initialized prior to program start up.
>
> Right -- and that's possible because the initializer for a static object
> has to be constant.
>
>> The key difference between "int x" (or "int x=10") and "static int x"
>> (with or without explicit initialization) is that during execution the
>> former causes the variable to be created (and initialized if
>> specified) while the latter does absolutely nothing since the variable
>> has already been created and initialized.
>>
>> A hidden flag is not needed at all.
>
> Right.
>
> <OT> C++ allows static objects to have non-constant initialization, so
> it does have to ensure that the initialization happens only the first
> time the declaration is encountered. A hidden flag is the obvious way
> to implement that behavior. </OT>
>

That might be what I am remembering.

For code like:

int f(int x) {
static int y = x;
return x-y;
}

The initialization for y, needs to be delayed until the first call to f,
so needs a flag.

Re: some observation about declaration in loop

<lr996hhlj2jfpec89v9fs2hje9v3ncr3t0@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21427&group=comp.lang.c#21427

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sat, 23 Apr 2022 18:27:47 -0700
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <lr996hhlj2jfpec89v9fs2hje9v3ncr3t0@4ax.com>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com> <87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com> <e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid> <877d7g10vh.fsf@bsb.me.uk> <332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com> <BFW8K.630276$LN2.557300@fx13.iad> <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com> <t41mhb$17dlk$1@news.mixmin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="2ad4a1146a67904efd39cc65245edf19";
logging-data="12132"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//oZKg5AmVJztfpHn5ppJAk1hHFBoIWYk="
Cancel-Lock: sha1:Qehbw8GL1KDawFlNEjMk63hJcEs=
X-Newsreader: Forte Agent 4.2/32.1118
 by: Barry Schwarz - Sun, 24 Apr 2022 01:27 UTC

On Sat, 23 Apr 2022 21:08:44 +0100, Sams Lara <samlara622@gmail.com>
wrote:

>On 23/04/2022 20:49, Barry Schwarz wrote:
>> while the latter does absolutely nothing since the variable
>> has already been created and initialized.
>>
>>
>In other words no memory is allocated at this stage.

Because the memory has already been allocated.

--
Remove del for email

Re: some observation about declaration in loop

<2b179189-8a29-40e2-85dc-5b75e90138f0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21428&group=comp.lang.c#21428

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:244f:b0:69e:a39c:3669 with SMTP id h15-20020a05620a244f00b0069ea39c3669mr6897070qkn.500.1650777278423;
Sat, 23 Apr 2022 22:14:38 -0700 (PDT)
X-Received: by 2002:a05:620a:13a3:b0:69e:13f4:6f96 with SMTP id
m3-20020a05620a13a300b0069e13f46f96mr6911810qki.100.1650777278288; Sat, 23
Apr 2022 22:14:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 22:14:38 -0700 (PDT)
In-Reply-To: <87ee1ncu5f.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=87.112.250.40; posting-account=3b1IMQoAAABTaHj6KDu9rDX4VoUvTgIu
NNTP-Posting-Host: 87.112.250.40
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<97afe458-a302-4cd0-89dc-e6416daf2948n@googlegroups.com> <87ee1ncu5f.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2b179189-8a29-40e2-85dc-5b75e90138f0n@googlegroups.com>
Subject: Re: some observation about declaration in loop
From: lorif32...@gmail.com (Lori Fairhead)
Injection-Date: Sun, 24 Apr 2022 05:14:38 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 33
 by: Lori Fairhead - Sun, 24 Apr 2022 05:14 UTC

On Saturday, April 23, 2022 at 11:27:53 PM UTC+1, Keith Thompson wrote:
> Lori Fairhead <lorif...@gmail.com> writes:
> [...]
> > Just think of it as FOR parameters DO command
> >
> > Each time the paramters tell FOR to execute the command
> >
> > In this case the command is a BLOCK so the block is executed
> > again and again for each iteration of the FOR loop.
> >
> > You can have a BLOCK anywhere you have a command like several other
> > languages.
> It's helpful to use the standard terminology. C has *statements*, not
> *commands*.
>
Well, whatever, I did say "Just think of it as.. ". Trying to clarify the *idea* not even
just C but generally to aid the OP

Anyhow I was also going to add that this was quite a logical concept
which got me thinking but I need to get a new copy of the C standard
to revise it... about just how logical C was but clang here does compile

int v;
v=printf("hello");
for(;v<100;v++) 2;

unlike some other language a block does not return a value

REGARDS LF

> --
> Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
> Working, but not speaking, for Philips
> void Void(void) { Void(); } /* The recursive call of the void */

Re: some observation about declaration in loop

<871qxnc8q6.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21430&group=comp.lang.c#21430

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sat, 23 Apr 2022 23:10:25 -0700
Organization: None to speak of
Lines: 51
Message-ID: <871qxnc8q6.fsf@nosuchdomain.example.com>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<97afe458-a302-4cd0-89dc-e6416daf2948n@googlegroups.com>
<87ee1ncu5f.fsf@nosuchdomain.example.com>
<2b179189-8a29-40e2-85dc-5b75e90138f0n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="de37a51cdfcc9bf522499568c2f037fc";
logging-data="9271"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+J8st2oKCi3qYI1P7pwss1"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:u9lNNZsYJ/tk/uVaFHItk1TxrtE=
sha1:LoeM27DeKTXiDSyFkTCjE9zhmiA=
 by: Keith Thompson - Sun, 24 Apr 2022 06:10 UTC

Lori Fairhead <lorif32768@gmail.com> writes:
> On Saturday, April 23, 2022 at 11:27:53 PM UTC+1, Keith Thompson wrote:
>> Lori Fairhead <lorif...@gmail.com> writes:
>> [...]
>> > Just think of it as FOR parameters DO command
>> >
>> > Each time the paramters tell FOR to execute the command
>> >
>> > In this case the command is a BLOCK so the block is executed
>> > again and again for each iteration of the FOR loop.
>> >
>> > You can have a BLOCK anywhere you have a command like several other
>> > languages.
>> It's helpful to use the standard terminology. C has *statements*, not
>> *commands*.
>>
> Well, whatever, I did say "Just think of it as.. ". Trying to clarify the *idea* not even
> just C but generally to aid the OP

If the OP is going to be programming in C, they will benefit greatly
from being able to communicate clearly about it. The term "statement"
is unambiguous. "Command" is not, and is likely to cause confusion.

> Anyhow I was also going to add that this was quite a logical concept
> which got me thinking but I need to get a new copy of the C standard
> to revise it... about just how logical C was but clang here does compile
>
> int v;
> v=printf("hello");
> for(;v<100;v++) 2;

Yes, that's perfectly valid code. C doesn't prevent you from writing
valid code that doesn't do anything useful. (I don't know of any
language that does, though most languages are more restrictive than C.)

Any expression followed by a semicolon is a valid statement. The
expression is evaluated, and its result is discarded. Normally the
expression will have side effects (for example an assignment expression
or a function call), but there's no requirement that it must.

Having said that, compilers will typically warn about statements with no
effect. Both gcc and clang will do so with the right options.

> unlike some other language a block does not return a value
>
> REGARDS LF

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: some observation about declaration in loop

<AABiZRdpRlIAAAuR.A2.flnews@Server4.micha.freeshell.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21431&group=comp.lang.c#21431

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: michael....@gmx.net (Michael Bäuerle)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sun, 24 Apr 2022 09:24:57 -0000
Lines: 59
Message-ID: <AABiZRdpRlIAAAuR.A2.flnews@Server4.micha.freeshell.org>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com> <87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com> <e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid> <877d7g10vh.fsf@bsb.me.uk> <332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com> <BFW8K.630276$LN2.557300@fx13.iad> <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com> <87a6cbctr5.fsf@nosuchdomain.example.com> <kY%8K.632211$7F2.240543@fx12.iad>
Mime-Version: 1.0
X-Trace: individual.net B3wMTorox+N8JyQJ1pM+DANGve2IeZtl2xqCjnm2u9h0EVYMVR
X-Orig-Path: not-for-mail
Cancel-Lock: sha256:KaJsDd/Iwtlf/P+z+Dnvef1amL0bEaVCLgYYyT1U7MM= sha1:1xVNiAd9NzgVRJun1/k/hXxLtwM=
Injection-Date: Sun, 24 Apr 2022 09:24:57 -0000
User-Agent: flnews/1.0.1 (for HP-UX)
 by: Michael Bäuerle - Sun, 24 Apr 2022 09:24 UTC

Richard Damon wrote:
> On 4/23/22 6:36 PM, Keith Thompson wrote:
> > Barry Schwarz <schwarzb@delq.com> writes:
> > > Richard Damon <Richard@Damon-Family.org> wrote:
> > > >
> > > > [...]
> > > > static int x = 10;
> > > >
> > > > Then by the rules of the C language, that initialization will only
> > > > happen the first time that we reach that statement, as after that the
> > > > variable has already been created, so it isn't initiialized again.
> > > >
> > > > In fact, behind the scenes, there needs to be some other hidden flag
> > > > created by the implementation to know if initialization has happened.
> > >
> > > I disagree. Paragraph 5.1.2 of C11 explicitly states that a static
> > > variable is initialized prior to program start up.
> >
> > Right -- and that's possible because the initializer for a static object
> > has to be constant.
> >
> > > The key difference between "int x" (or "int x=10") and "static int x"
> > > (with or without explicit initialization) is that during execution the
> > > former causes the variable to be created (and initialized if
> > > specified) while the latter does absolutely nothing since the variable
> > > has already been created and initialized.
> > >
> > > A hidden flag is not needed at all.
> >
> > Right.
> >
> > <OT> C++ allows static objects to have non-constant initialization, so
> > it does have to ensure that the initialization happens only the first
> > time the declaration is encountered. A hidden flag is the obvious way
> > to implement that behavior. </OT>
>
> That might be what I am remembering.
>
> For code like:
>
> int f(int x) {
> static int y = x;
> return x-y;
> }
>
> The initialization for y, needs to be delayed until the first call to
> f, so needs a flag.

According to C11 Paragraph 5.1.2, the variable must already be
initialized when f() is called for the first time (at program startup,
see above).

Even if I see no explicit statement in C11, that the initializer must
be constant, using variable x is not possible because the value of x
may be unknown at program startup.

If I try to compile your code above, the compiler says:
|
| error: initializer element is not constant

Re: some observation about declaration in loop

<7969bfd6-dce4-43fa-918a-f3fbc9d2889bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21433&group=comp.lang.c#21433

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:c6a:b0:452:b9b:c68f with SMTP id t10-20020a0562140c6a00b004520b9bc68fmr7970860qvj.46.1650799656230;
Sun, 24 Apr 2022 04:27:36 -0700 (PDT)
X-Received: by 2002:a05:6214:262a:b0:456:354e:21e4 with SMTP id
gv10-20020a056214262a00b00456354e21e4mr952298qvb.110.1650799656123; Sun, 24
Apr 2022 04:27:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 24 Apr 2022 04:27:35 -0700 (PDT)
In-Reply-To: <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.136; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.136
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk> <332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad> <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7969bfd6-dce4-43fa-918a-f3fbc9d2889bn@googlegroups.com>
Subject: Re: some observation about declaration in loop
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 24 Apr 2022 11:27:36 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 71
 by: fir - Sun, 24 Apr 2022 11:27 UTC

sobota, 23 kwietnia 2022 o 21:49:51 UTC+2 Barry Schwarz napisał(a):
> On Sat, 23 Apr 2022 13:26:56 -0400, Richard Damon
> <Ric...@Damon-Family.org> wrote:
>
> >The key difference between
> >
> >int x = 10;
> >
> >and
> >
> >int x;
> >x = 10;
> >
> >is that the first INITIALIZES x when it is created, and the second
> >ASSIGNS it when the statement is executed.
> >
> >When we change that to
> >
> >static int x = 10;
> >
> >Then by the rules of the C language, that initialization will only
> >happen the first time that we reach that statement, as after that the
> >variable has already been created, so it isn't initiialized again.
> >
> >In fact, behind the scenes, there needs to be some other hidden flag
> >created by the implementation to know if initialization has happened.
> I disagree. Paragraph 5.1.2 of C11 explicitly states that a static
> variable is initialized prior to program start up.
>
> The key difference between "int x" (or "int x=10") and "static int x"
> (with or without explicit initialization) is that during execution the
> former causes the variable to be created (and initialized if
> specified) while the latter does absolutely nothing since the variable
> has already been created and initialized.
>
> A hidden flag is not needed at all.
>

in fact when i thinked of it it showed to me that behaviour of
static int x = 10; may be understood - as realization what initialisation is and what
static is
behaviour of int x = 10; in loop hovever comes probably from different point it is practical
usability (i am not sure though, int x = 10 done to restet to 10 each time is usable but
im not sure how int x = 10 which not resets is usable)
in fact if you want in loop make x that is initialised once and then reset it you could put
int x = 10; if that would only initialize once and x = 10 when you need reset
in form as it is now if im not wrong you cant do it becouse you cant put initialisation
in loop that only assign once, so you need to put it before whole loop which is definitivly bad

see

while(x<100)
{ int x = 10;
x++;
} if thios assign only once you can write loop 10..99 this way, if it resets as today you cant and
you need to move x out the block (which breaks code locality)

(i say this assuming im not wrong if i made some mistake someone could correct me, as i wouldnt not put someone to wrong state of thinking if im mistaken, but seems probably im ok here)

Re: some observation about declaration in loop

<b34565b8-e579-42c1-bbb0-d6628153a04an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21434&group=comp.lang.c#21434

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:7d4d:0:b0:2f1:fcbc:b8a1 with SMTP id h13-20020ac87d4d000000b002f1fcbcb8a1mr8831305qtb.567.1650799983795;
Sun, 24 Apr 2022 04:33:03 -0700 (PDT)
X-Received: by 2002:a05:6214:2308:b0:432:e69f:5d71 with SMTP id
gc8-20020a056214230800b00432e69f5d71mr9315555qvb.19.1650799983641; Sun, 24
Apr 2022 04:33:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 24 Apr 2022 04:33:03 -0700 (PDT)
In-Reply-To: <2a60031d-9645-4f13-b105-544eaeefca8bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.136; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.136
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com> <2a60031d-9645-4f13-b105-544eaeefca8bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b34565b8-e579-42c1-bbb0-d6628153a04an@googlegroups.com>
Subject: Re: some observation about declaration in loop
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 24 Apr 2022 11:33:03 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 46
 by: fir - Sun, 24 Apr 2022 11:33 UTC

niedziela, 24 kwietnia 2022 o 00:27:40 UTC+2 bart c napisał(a):
> On Saturday, 23 April 2022 at 01:43:00 UTC+1, fir wrote:
> > may seem funny but i tend to always not remember how declaration of in in loop behaves (though im not c newb for sure)
> >
> > for(int i=0; i<10; i++)
> > {
> > int x = 10;
> > x++;
> > printf("\n %d", x);
> > }
> >
> > for(int i=0; i<10; i++)
> > {
> > static int x = 10;
> > x++;
> > printf("\n %d", x);
> > }
> >
> > those two behaves quite differently (though i test in c++ mode must say but i assume in standard c it is the same)
> >
> > are you able to justify/explain the way it behaves is the right way?
> Inside a function:
>
> int x = 10; // initialised to 10 each time it is encountered (eg. per loop)
>
> static int x = 10; // initialised to 10 at load-time (actually, set in ..data section of exe)
>
> If this was written with my syntax, it would clearer as the first is written using `x := 10`, and the second using `x = 10`. `:=` means a runtime assignment. `=` is taken care at compile-time/load-time.

weird..in context of c i think much better syntax is this x={10} for initialisation
hovever it would make that assigning structures like x = {1,2,3}; would not work in runtime

as i said imo the assign sign in initialisation not quite fits at all so maybe it would be better to
note initialisation as x{10} that would make live such form of whiles

while(int x{10}+=2<100)
it allows mixing initialisation and assignment in place, hovever
while(int x={10}+=2<100) or some varioations of it also allows that

Re: some observation about declaration in loop

<7cc9f316-e477-40b4-a60a-fec2fdba1b0en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21435&group=comp.lang.c#21435

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1488:b0:2f3:6457:b555 with SMTP id t8-20020a05622a148800b002f36457b555mr2204214qtx.530.1650800704299;
Sun, 24 Apr 2022 04:45:04 -0700 (PDT)
X-Received: by 2002:a05:6214:c44:b0:44d:adfa:612e with SMTP id
r4-20020a0562140c4400b0044dadfa612emr9071449qvj.3.1650800704182; Sun, 24 Apr
2022 04:45:04 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 24 Apr 2022 04:45:03 -0700 (PDT)
In-Reply-To: <7969bfd6-dce4-43fa-918a-f3fbc9d2889bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.136; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.136
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk> <332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad> <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
<7969bfd6-dce4-43fa-918a-f3fbc9d2889bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7cc9f316-e477-40b4-a60a-fec2fdba1b0en@googlegroups.com>
Subject: Re: some observation about declaration in loop
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 24 Apr 2022 11:45:04 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 46
 by: fir - Sun, 24 Apr 2022 11:45 UTC

niedziela, 24 kwietnia 2022 o 13:27:42 UTC+2 fir napisał(a):
> >
> in fact when i thinked of it it showed to me that behaviour of
> static int x = 10; may be understood - as realization what initialisation is and what
> static is
> behaviour of int x = 10; in loop hovever comes probably from different point it is practical
> usability (i am not sure though, int x = 10 done to restet to 10 each time is usable but
> im not sure how int x = 10 which not resets is usable)
> in fact if you want in loop make x that is initialised once and then reset it you could put
> int x = 10; if that would only initialize once and x = 10 when you need reset
> in form as it is now if im not wrong you cant do it becouse you cant put initialisation
> in loop that only assign once, so you need to put it before whole loop which is definitivly bad
>
> see
>
> while(x<100)
> {
> int x = 10;
> x++;
> }

well this would also needed that while header has acces to inside block variables but it could be other example

while(1)
{
int x = 10;
x++;
if(x==99) break;
}
and i think such constructions are probably practically usable

> if thios assign only once you can write loop 10..99 this way, if it resets as today you cant and
> you need to move x out the block (which breaks code locality)
>
> (i say this assuming im not wrong if i made some mistake someone could correct me, as i wouldnt not put someone to wrong state of thinking if im mistaken, but seems probably im ok here)

Re: some observation about declaration in loop

<87wnfeb2yl.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21443&group=comp.lang.c#21443

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Sun, 24 Apr 2022 14:12:34 -0700
Organization: None to speak of
Lines: 24
Message-ID: <87wnfeb2yl.fsf@nosuchdomain.example.com>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
<87czh81tmt.fsf@bsb.me.uk>
<ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com>
<e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid>
<877d7g10vh.fsf@bsb.me.uk>
<332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com>
<BFW8K.630276$LN2.557300@fx13.iad>
<sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com>
<87a6cbctr5.fsf@nosuchdomain.example.com>
<kY%8K.632211$7F2.240543@fx12.iad>
<AABiZRdpRlIAAAuR.A2.flnews@Server4.micha.freeshell.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="de37a51cdfcc9bf522499568c2f037fc";
logging-data="20958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19paCHYeIa+VyWQDF7AdiBr"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:fwRYn0mN3bGnQ+XTn5xtfK+p6uM=
sha1:6OBVGVVwEY9kW3W73bLWfRMPAcg=
 by: Keith Thompson - Sun, 24 Apr 2022 21:12 UTC

Michael Bäuerle <michael.baeuerle@gmx.net> writes:
[...]
> According to C11 Paragraph 5.1.2, the variable must already be
> initialized when f() is called for the first time (at program startup,
> see above).
>
> Even if I see no explicit statement in C11, that the initializer must
> be constant, using variable x is not possible because the value of x
> may be unknown at program startup.

N1570 6.7.9p4, under "Constraints":

All the expressions in an initializer for an object that has static
or thread storage duration shall be constant expressions or string
literals.

> If I try to compile your code above, the compiler says:
> |
> | error: initializer element is not constant

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: some observation about declaration in loop

<AABiZmESAMgAAALT.A3.flnews@WStation5.stz-e.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21452&group=comp.lang.c#21452

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: michael....@stz-e.de (Michael Bäuerle)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Date: Mon, 25 Apr 2022 10:51:30 +0200 (CEST)
Lines: 19
Message-ID: <AABiZmESAMgAAALT.A3.flnews@WStation5.stz-e.de>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com> <87czh81tmt.fsf@bsb.me.uk> <ac67c805-e5cf-489b-ab72-9effbee3e1ccn@googlegroups.com> <e5d26ae3-f4fe-4531-8482-922dd1992eaf@hooterville.invalid> <877d7g10vh.fsf@bsb.me.uk> <332337a6-63db-4ee9-b6a4-47f08f5484ebn@googlegroups.com> <BFW8K.630276$LN2.557300@fx13.iad> <sml86h115ablteo7kjlil7dkedhr853s2n@4ax.com> <87a6cbctr5.fsf@nosuchdomain.example.com> <kY%8K.632211$7F2.240543@fx12.iad> <AABiZRdpRlIAAAuR.A2.flnews@Server4.micha.freeshell.org> <87wnfeb2yl.fsf@nosuchdomain.example.com>
Reply-To: Michael Bäuerle <michael.baeuerle@gmx.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=fixed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net pjKEnUULy7q8e/Kq0lRJTQgIvx1iFJkjsHpqOUG757gh2hwHDS
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:otSTa8fCaEspmJ3+pv4kpuxTcI0= sha256:hsuHG9Lg8Lq4+9A5A+IflFSk/mzGo9LgZMdrNXTWFKI= sha1:oOhCpn5jfcRh3dpJ7b8RGbDfClE=
Injection-Date: Mon, 25 Apr 2022 08:51:30 -0000
User-Agent: flnews/1.1.0pre14 (for NetBSD)
 by: Michael Bäuerle - Mon, 25 Apr 2022 08:51 UTC

Keith Thompson wrote:
> Michael Bäuerle <michael.baeuerle@gmx.net> writes:
> >
> [...]
> > According to C11 Paragraph 5.1.2, the variable must already be
> > initialized when f() is called for the first time (at program startup,
> > see above).
> >
> > Even if I see no explicit statement in C11, that the initializer must
> > be constant, using variable x is not possible because the value of x
> > may be unknown at program startup.
>
> N1570 6.7.9p4, under "Constraints":
>
> All the expressions in an initializer for an object that has static
> or thread storage duration shall be constant expressions or string
> literals.

Thank you. I have overlooked this paragraph.

Re: some observation about declaration in loop

<t46o6e$9sf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21465&group=comp.lang.c#21465

  copy link   Newsgroups: comp.lang.c
Followup: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jfbode1...@gmail.com (John Bode)
Newsgroups: comp.lang.c
Subject: Re: some observation about declaration in loop
Followup-To: comp.lang.c
Date: Mon, 25 Apr 2022 13:10:22 -0500
Organization: A noiseless patient Spider
Lines: 91
Message-ID: <t46o6e$9sf$1@dont-email.me>
References: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Apr 2022 18:10:22 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8c76a2cf2643b391d6d9dcf10fd2075f";
logging-data="10127"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197D28XOiRzLAM/LrO92mCC"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.8.0
Cancel-Lock: sha1:2RqFDa7UqK8RJwd3eQIxRi5gyN0=
In-Reply-To: <194cbd97-9fad-4958-927c-b48c2cfe2df5n@googlegroups.com>
Content-Language: en-US
 by: John Bode - Mon, 25 Apr 2022 18:10 UTC

On 4/22/22 7:42 PM, fir wrote:
> may seem funny but i tend to always not remember how declaration of in in loop behaves (though im not c newb for sure)
>
> for(int i=0; i<10; i++)
> {
> int x = 10;

In this loop the object designated by 'x' has "auto" storage duration -
its lifetime is limited to a single loop iteration. Logically speaking,
a new instance of 'x' is created and initialized at the beginning of the
loop body and destroyed at the end of the loop body[1].

> x++;
> printf("\n %d", x);
> }
>
> for(int i=0; i<10; i++)
> {
> static int x = 10;

In this loop the object designated by 'x' has "static" storage duration
(thanks to the 'static' keyword) - its lifetime is the same as the
overall program. The object designated by 'x' will be created and
initialized once at program startup and destroyed when the program
terminates.

> x++;
> printf("\n %d", x);
> }
>
> those two behaves quite differently (though i test in c++ mode must say but i assume in standard c it is the same)
>
> are you able to justify/explain the way it behaves is the right way?

Chapter and verse (N1570):

> 6.2.4 Storage durations of objects
>
> ...
>
> 3 An object whose identifier is declared without the storage-class
> specifier _Thread_local, and either with external or internal linkage
> or with the storage-class specifier static, has static storage
> duration. Its lifetime is the entire execution of the program and its
> stored value is initialized only once, prior to program startup.
>
> ...
>
> 5 An object whose identifier is declared with no linkage and without
> the storage-class specifier static has automatic storage duration, as
> do some compound literals. The result of attempting to indirectly
> access an object with automatic storage duration from a thread other
> than the one with which the object is associated is implementation-
> defined.
>
> 6 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. (Entering an
> enclosed block or calling a function suspends, but does not end,
> execution of the current block.) If the block is entered recursively,
> a new instance of the object is created each time. The initial value
> of the object is indeterminate. If an initialization is specified for
> the object, it is performed each time the declaration or
> compound literal is reached in the execution of the block; otherwise,
> the value becomes indeterminate each time the declaration is reached.

So, if you're seeing the first loop print

11
11
11
11
11
...

and the second loop print

11
12
13
14
15
...

then yes, it's behaving as expected.

------------------

[1] In practice a single memory location will be used rather than
allocating and deallocating memory with each iteration, it will just be
re-initialized each time through the loop.

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor