Udostępnij za pośrednictwem


Simple Variable Declarations

The declaration of a simple variable, the simplest form of a direct declarator, specifies the variable's name and type. It also specifies the variable's storage class and data type.

Storage classes or types (or both) are required on variable declarations. Untyped variables (such as var;) generate warnings.

Syntax

  • declarator:
    pointer opt

    direct-declarator

  • direct-declarator:
    identifier

  • identifier:
    nondigit

    identifier nondigit

    identifier digit

For arithmetic, structure, union, enumerations, and void types, and for types represented by typedef names, simple declarators can be used in a declaration since the type specifier supplies all the typing information. Pointer, array, and function types require more complicated declarators.

You can use a list of identifiers separated by commas (,) to specify several variables in the same declaration. All variables defined in the declaration have the same base type. For example:

int x, y;        /* Declares two simple variables of type int */
int const z = 1; /* Declares a constant value of type int */

The variables x and y can hold any value in the set defined by the int type for a particular implementation. The simple object z is initialized to the value 1 and is not modifiable.

If the declaration of z was for an uninitialized static variable or was at file scope, it would receive an initial value of 0, and that value would be unmodifiable.

unsigned long reply, flag; /* Declares two variables
                              named reply and flag     */

In this example, both the variables, reply and flag, have unsigned long type and hold unsigned integral values.

See Also

Concepts

Declarators and Variable Declarations