on1 construct

Brad Spear brad at sdcrdcf.UUCP
Thu Nov 8 05:03:04 AEST 1984

In article <124 at cadvax> jacob at cadvax.UUCP (Jacobo Bulaevsky) writes:
>I've started functions in the following way:
>function ()
>    static char first_time_in = TRUE;
>    if (first_time_in)  {
>        first_time_in = FALSE;
>	bla, bla, bla...
>    };
>    etc...

Your letter got me to thinking, I have to start some procedures that way also.
The following is what I came up with.  I tested it with the simple program
that follows, and it appeared to work, so we'll see.

#define TRUE    1
#define FALSE   0
#define ON_FIRST_TIME   static char _done = FALSE; if (!_done && (_done=TRUE))

{       int j;

	for (j = 0; j < 3; j++) sub();

	ON_FIRST_TIME printf ("inside init\n");
	printf("routine call\n");

Note that ON_FIRST_TIME would be treated like any 'if' statement, so just
put in some '{}', and off you go.  The only restriction is that it must
be the first expression in a block, because of the declaration of done.

Note that if the expression "!_done" evaluates to FALSE, which it does after
the first time, the expression "(_done=TRUE)" will not be re-evaluated.

If you want an expression that doesn't use a hidden variable, you might try
this one, although I haven't tested it.

#define ON_FIRST_TIME(x)        if (!(x) && ((x)=TRUE))

In this case, the variable used for x would already have to be declared, but
it also allows the macro to be used anywhere.

Brad Spear

More information about the Comp.lang.c mailing list