USDA ARS

Use of "static"

The static keyword is unfortunately overloaded in C, affecting both the visibility and the extent of a declaration [Harbison 1987]. In IPW, there are three situations in which the static storage class may be used.

First, top-level definitions may be made static to keep them from being accessed outside the module in which they appear.

Second, local variables may be made static if they are aggregates that must be initialized, or if their addresses are required to initialize other aggregates. This use of static is common in IPW; e.g., with the OPTION_T type to collect program arguments, and with the GETHDR_T type to control input image header processing.

Example:

{
        static OPTION_T opt_b = {
                'b ' , "begin line,sample",
                REAL_OPTARGS, "coord",
                OPTIONAL, 2, 2
        };

        ...

        static OPTION_T operands = {
                OPERAND, "input image file",
                STR_OPERANDS, "image",
                OPTIONAL, 1, 1,
        };

        static OPTION_T *optv[] = {
                &opt_b,
                ...
                &operands,
                0
        };
}

Finally, local variables may be made static to preserve their values between calls to the function in which they are defined. This method is used chiefly for one-time initialization of dynamic data structures:

Example:

int
foo(void)
{
        static bool_t        already = FALSE;

        if (!already) {
                already = TRUE;

                /* initialization code goes here */
        }
}

Local static variables should never be used as "implicit" arguments (i.e., to affect the behavior of the function as perceived by its caller.) static definitions should not appear in nested blocks (i.e., anywhere within a block except at the beginning of a function).


IPW documentation / Last revised 20 May 2009 / IPW web site