Article View: comp.std.c
Article #2347Re: Initialization of automatics within loops
From: Doug Gwyn
Date: Tue, 05 Feb 1991 19:57
Date: Tue, 05 Feb 1991 19:57
27 lines
1656 bytes
1656 bytes
In article <1991Feb5.023809.389086@locus.com> geoff@locus.com (Geoff Kuenning) writes:
- while (i < 3)
- {
- int j = 4;
- printf ("i = %d, j = %d\n", i, j);
- i++;
- j++;
- }
-What does this print? On the machine I'm using, it prints the same value
-for j (j = 4) three times. On the other hand, the programmer of XSend()
-clearly expected three different values for j.
The programmer clearly made a mistake. j has the value 4 for each
execution of the printf(). I was unable to tell from the code
snippet just what the programmer intended; merely changing the
storage class of j to "static" would probably not be correct either.
-I just checked the ANSI C spec on this, and found it unclear. It is
-explicitly stated that automatics are initialized on every entry to a
-compound statement. However, it is not made clear whether the construct:
- while (<expression>)
- <statement>
-is considered to *re-enter* the compound statement every time or not.
There is no question whatsoever about this. The compound statement is
completely evaluated for each iteration; this includes the auto
initialization.
Technical Details
Message-ID:
<15086@smoke.brl.mil>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!gatech!purdue!haven!adm!smoke!gwynReferences:
<1991Feb5.023809.389086@locus.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!gatech!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.std.c,comp.lang.c Subject: Re: Initialization of automatics within loops Message-ID: <15086@smoke.brl.mil> Date: 5 Feb 91 19:57:35 GMT References: <1991Feb5.023809.389086@locus.com> Followup-To: comp.std.c Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 27