[net.lang.c] Is this legal C?

dave@utcsrgv.UUCP (Dave Sherman) (11/21/83)

I recently had occasion to use a construct which is a little strange,
and I was wondering whether it's guaranteed to be portable.

		if(0) retry: printf("Try again.\n");
		printf("What's two plus two? ");
		....
		/* if get a bad answer */
		goto retry;

This code ensures that the "Try again" message isn't executed the
first time through. It works fine on the VAX (4.1bsd) and PDP-11 (v7).
Can I be sure that the "goto" to a statement within a condition
will work with any C compiler?

Dave Sherman
-- 
 {allegra,cornell,decvax,ihnp4,linus,utzoo}!utcsrgv!dave

chris@umcp-cs.UUCP (11/23/83)

	if (0) retry: printf ("foo\n");
	...
	goto retry;

Looks like legal C to me, although I suspect that it's more likely to
compile on every compiler if you put { } around the "retry: printf"
part... (just because someone's compiler may interpret a label as a
statement -- which it is not, but that makes no difference in most
cases).

In fact, the ability to write this kind of thing is one of the big
gripes that some optimizer-people have with C.  These people hate
unrestricted gotos.  (Admittedly the example is really a restricted
goto and could be optimized.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris.umcp-cs@CSNet-Relay

smh@mit-eddie.UUCP (11/23/83)

References: utcsrgv.2783 <2454@yale-com.UUCP>
Relay-Version:version B 2.10.1 6/24/83; site duke.UUCP
Posting-Version:version B 2.10.1 6/24/83; site mit-eddie.UUCP
Path:duke!decvax!genrad!mit-eddie!smh
Message-ID:<951@mit-eddie.UUCP>
Date:Wed, 23-Nov-83 07:25:42 EST
Organization:MIT, Cambridge, MA

A minor quibble with Jerry Leichter, who writes:
---------------------------------------------
Consider setjmp/longjmp.  The standard way to use them is:

	if (setjmp(&buf) == 0)
	{	do something	}
	else
	{	handle the low-level error	}

This has the same control-passing structure as your goto example, once
the stack has been unwound.  Now, setjmp/longjmp are not part of the
language; but they have become pretty universal, and any compiler that
made them unimplementable would likely not gain much of a following.
---------------------------------------------
To the compiler there is a significant difference between a label on
an `if' conditional and the `setjmp' function call.  The former
requires the compiler generate code (if only an assembler label) while
the latter is a function which `magically' bashes -- whoops, I mean
restores -- the stack environment identically when `longjmp' is later
called.  `setjmp' is part of the object library environment, not the
language, and it would be incorrect for any C *compiler* to treat it
specially.

Steve Haflich, MIT Experimental Music Studio

leichter@yale-com.UUCP (Jerry Leichter) (11/28/83)

Steve Haflich missed my point.  No C compiler I know of does anything special
with setjmp/longjmp.  The point is exactly that it doesn't have to:  The code
produced is consistent with these functions.  If the code produced for an
if did NOT allow a goto into one arm, it would almost certainly make setjmp/
longjmp unimplementable.  While this MIGHT be consistent with the formal
definition of C, it would not be acceptable in the real world of C programming
practice, which has come to rely on setjmp/longjmp.
							-- Jerry