Initialization of automatics within loops

Geoff Kuenning geoff at locus.com
Tue Feb 5 13:38:09 AEST 1991


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 (<expression>)
	    <statement>

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 at la.locus.com	geoff at ITcorp.com



More information about the Comp.lang.c mailing list