Xref: utzoo comp.std.c:4239 comp.lang.c:35794 comp.windows.x:32293 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!turnkey!orchard.la.locus.com!fafnir.la.locus.com!geoff From: geoff@locus.com (Geoff Kuenning) Newsgroups: comp.std.c,comp.lang.c,comp.windows.x Subject: Initialization of automatics within loops Message-ID: <1991Feb5.023809.389086@locus.com> Date: 5 Feb 91 02:38:09 GMT Organization: Locus Computing Corp, Los Angeles Lines: 43 The following little program demonstrates a problem found in the XSend() routine of XlibInt.c: main () { int i = 0; 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. 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 () is considered to *re-enter* the compound statement every time or not. My instinct says yes, the statement is re-entered, but obviously this interpretation is open to discussion. In terms of usefulness, I could argue that either option is useful to programmers. What do people think the spec should do about this question? (BTW, the spec explicitly says that implementations get to decide whether automatics are initialized upon jumps into a compound statement, so perhaps this issue should also be implementation-defined). I'm cross-posting to comp.windows.x so that X-Windows users will be made aware of a potential bug (the problem will show up only under certain very rare conditions, so it could be lurking in your system without your knowledge). -- Geoff Kuenning geoff@la.locus.com geoff@ITcorp.com