Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.