United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
Compaq C

Compaq C
Language Reference Manual


Previous Contents Index


Chapter 4
Declarations

Declarations are used to introduce the identifiers used in a program and to specify their important attributes, such as type, storage class, and identifier name. A declaration that also causes storage to be reserved for an object or that includes the body of a function, is called a definition.

Section 4.1 covers general declaration syntax rules, Section 4.2 discusses initialization, and Section 4.3 describes external declarations.

The following kinds of identifiers can be declared. See the associated section for information on specific declaration and initialization syntax. Functions are discussed in Chapter 5.

Note

Preprocessor macros created with the #define directive are not declarations. Chapter 8 has information on creating macros with preprocessor directives.

4.1 Declaration Syntax Rules

The general syntax of a declaration is as follows:

declaration:

declaration-specifiers init-declarator-listopt;

declaration-specifiers:

storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt

init-declarator-list:

init-declarator
init_declarator-list , init-declarator

init-declarator:

declarator
declarator = initializer

Note the following items about the general syntax of a declaration:

Consider the following example:


volatile static int data = 10; 

This declaration shows a qualified type (a data type with a type qualifier -- in this case, int qualified by volatile ), a storage class ( static ), a declarator ( data ), and an initializer ( 10 ). This declaration is also a definition, because storage is reserved for the data object data .

The previous example is simple to interpret, but complex declarations are more difficult. See your platform-specific Compaq C documentation for more information about interpreting C declarations.

The following semantic rules apply to declarations:

Storage Allocation

Storage is allocated to a data object in the following circumstances:

Note

The compiler does not necessarily allocate distinct variables to memory locations according to the order of declaration in the source code. Furthermore, the order of allocation can change as a result of seemingly unrelated changes to the source code, command-line options, or from one version of the compiler to the next - it is essentially unpredictable. The only way to control the placement of variables relative to each other is to make them members of the same struct type.

4.2 Initialization

Initializers provide an initial value for objects, and follow this syntax:

initializer:

assignment-expr
{ initializer-list }
{ initializer-list, }

initializer-list:

designation-opt initializer
initializer-list, designation-opt initializer

designation:

designator-list =

designator-list:

designator
designator-list designator

designator:

[ constant-expr ]
. identifier

Initialization of objects of each type is discussed in the following sections, but a few universal constraints apply to all initializations in C:

C has historically allowed initializers to be optionally surrounded by extra braces (to improve formatting clarity, for instance). These initializers are parsed differently depending on the type of parser used. Compaq C uses the parsing technique specified by the ANSI standard, known as the top-down parse. Programs depending on a bottom-up parse of partially braced initializers can yield unexpected results. The compiler generates a warning message when it encounters unnecessary braces in common C compatibility mode or when the error-checking compiler option is specified on the command line.

4.3 External Declarations

An object declaration outside of a function is called an external declaration. Contrast this with an internal declaration, which is a declaration made inside a function or block; the declaration is internal to that function or block, and is visible only to that function or block. The compiler recognizes an internally declared identifier from the point of the declaration to the end of the block.

If an object's declaration has file scope and an initializer, the declaration is also an external definition for the object. A C program consists of a sequence of external definitions of objects and functions.

Any definition reserves storage for the entity being declared. For example:


float fvalue = 15.0;                /* external definition  */ 
main () 
{ 
  int ivalue = 15;                  /* internal definition  */ 
} 

External data declarations and external function definitions take the same form as any data or function declaration (see Chapter 5 for standard function declaration syntax), and must follow these rules:

Note

An external function can be called without previously declaring it in C, but this construction is not recommended because of the loss of type checking and subsequent susceptibility to bugs. If such a function call is made, the compiler will treat the function as if an external declaration of type int appeared in the block containing the call. For example:


void function1() 
{ 
int a,b; 
x (a,b); 
} 


Here, the compiler will behave as if the declaration extern int x(); appeared within the function1 definition block.

The first declaration of an identifier in a compilation unit must specify, explicitly or by the omission of the static keyword, whether the identifier is internal or external. For each object, there can be only one definition. Multiple declarations of the same object may be made, as long as there are no conflicting or duplicate definitions for the same object.

An external object may be defined with either an explicit initialization or a tentative definition. A declaration of an object with file scope, without an initializer, and with a storage-class specifier other than static is a tentative definition. The compiler will treat a tentative definition as the object's only definition unless a complete definition for the object is found. As with all declarations, storage is not actually allocated until the object is defined.

If a compilation unit contains more than one tentative definition for an object, and no external definition for the object, the compiler treats the definition as if there were a file scope declaration of the object with an initializer of zero, with composite type as of the end of the compilation unit. See Section 2.7 for a definition of composite type.

If the declaration of an object is a tentative definition and has internal linkage, the declared type must not be an incomplete type. See Section 2.9 for examples of tentative definitions.

4.4 Declaring Simple Objects

Simple objects are objects with one of the basic data types. Therefore, a simple object can have an integral or floating-point type. Like all objects, simple objects are named storage locations whose values can change throughout the execution of the program. All simple objects used in a program must be declared.

A simple object declaration can be composed of the following items:

4.4.1 Initializing Simple Objects

An initializer for a simple object consists of an equal sign (=) followed by a single constant expression. For example:


int x = 10; 
float y = ((12 - 2) + 25); 

Here, the declaration both declares and defines the object x as an integer value initially equal to 10, and declares and defines the floating-point value y with an initial value of 35.

Without an initializer, the initial value of an auto object is undefined. A static object without explicit initialization is automatically initialized to 0. (If the object is a static array or structure, all members are initialized to 0.)

A block scope identifier with external or internal linkage (that is, declared using the extern or static keywords) cannot include an initializer in the declaration, because it is initialized elsewhere.

4.4.2 Declaring Integer Objects

Integer objects can be declared with the int , long , short , signed , and unsigned keywords. char can also be used, but only for small values. The following statements are examples of integer declarations:


int x;        /*  Declares an integer variable x    */ 
int y = 10;   /*  Declares an integer variable y    */ 
              /*  and sets y's initial value to 10  */ 

Some of the keywords can be used together to explicitly state the allowed value range. For example:


unsigned long int a; 
signed long;           /*  Synonymous with "signed long int"   */ 
unsigned int; 

Consider the range of values an integer object must be capable of representing when selecting the integral data type for the object. See Chapter 3 for more information on the size and range of integral data types.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement