United States    
COMPAQ
Compaq C

Compaq C
Language Reference Manual


Previous Contents Index

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.

4.4.3 Declaring Character Variables

Character objects are declared with the char keyword. The following example shows a character declaration with the initialization of a character object:


char ch = 'a'; /* Declares an object ch with an initial value 'a' */ 

In C, character string literals are stored in arrays of type char . See Section 4.7 for more information on arrays.

4.4.4 Declaring Floating-Point Variables

When declaring floating-point objects, determine the amount of precision needed for the stored object. Single-precision or double-precision objects can be used. For single precision, use the float keyword. For double precision, use the double or long double keywords. For example:


float x = 7.5; 
double y = 3.141596; 

See your platform-specific Compaq C documentation for specific information on the range and precision of floating-point types.

4.5 Declaring Enumerations

An enumerated type is a user-defined integer type. An enumerated type defines enumeration constants, which are integral constant expressions with values that can be represented as integers. An enumerated type declaration follows this syntax:

enum-specifier:

enum identifieropt { enumerator-list}
enum identifieropt { enumerator-list , }
enum identifier

enumerator-list:

enumerator
enumerator-list, enumerator

enumerator:

enumeration-constant
enumeration-constant = constant_expression

In Compaq C, objects of type enum are compatible with objects of type signed int .

The following example shows the declaration of an enumeration type and an enumeration tag:


enum shades 
   { 
      off, verydim, dim, prettybright, bright 
   }  light; 

This declaration defines the variable light to be of an enumerated type shades . light can assume any of the enumerated values.

The tag shades is the enumeration tag of the new type. off through bright are the enumeration constants with values 0 through 4. These enumeration constants are constant values and can be used wherever integer constants are valid.

Once a tag is declared, it can be used as a reference to that enumerated type, as in the following declaration, where the variable light1 is an object of the enumerated data type shades :


enum  shades  light1; 

An incomplete type declaration of an enumerated type is illegal; for example:


enum e; 

An enum tag can have the same spelling as other identifiers in the same program in other name spaces. However, enum constant names share the same name space as variables and functions, so they must have unique names to avoid ambiguity.

Internally, each enumeration constant is associated with an integer constant; the compiler gives the first enumeration constant the value 0 by default, and the remaining enumeration constants are incremented by 1 for each succeeding value. Any enumeration constant can be set to a specific integer constant value. The enumeration constants following such a construct (unless they are also set to specific values) then receive values that are one greater than the previous value. Consider the following example:


enum spectrum 
   { 
      red, yellow = 4, green, blue, indigo, violet 
   }  color2 = yellow; 

This declaration gives red , yellow , green , blue ,..., the values 0, 4, 5, 6,... Assigning duplicate values to enumeration constants is permitted.

The value of color2 is an integer (4), not a string such as "red" or "yellow".

4.6 Declaring Pointers

Pointers are variables that contain the memory addresses of objects or functions. Pointer variables are declared as a pointer type by using the asterisk punctuator and the data type of the object pointed to, as shown in the following syntax:

pointer:

* type-qualifier-listopt
* type-qualifier-listopt pointer

type-qualifier-list:

type-qualifier
type-qualifier-list type-qualifier

By default, Compaq C pointers are 32 bits long on OpenVMS systems and 64 bits long on Tru64 UNIX systems. Although their defaults are different, both OpenVMS Alpha and Tru64 UNIX systems support 32-bit (short) and 64-bit (long) pointers. Compaq C provides qualifiers/switches and #pragma preprocessor directives to control pointer size.

The type-qualifier is either const , volatile , __unaligned (ALPHA), __restrict , or any combination thereof.

An object of pointer type is declared as in the following example:


char *px; 

In this example, identifier px is declared as a pointer to an object of type char . No type-qualifier is used in this example. The expression *px yields the char that px points to.

The following declarations show the difference between a variable pointer to a constant, a constant pointer to a variable, and a constant pointer to a constant object.


const int *ptr_to_constant;     /*  pointer variable pointing 
                                    to a const object         */ 
int *const constant_ptr;        /*  constant pointer to a 
                                    non-const object          */ 
const int *const constant_ptr;  /*  Const pointer to a 
                                    const object              */ 

The contents of an object pointed to by ptr_to_constant cannot be modified through that pointer, but ptr_to_constant itself can be changed to point to another const -qualified object. Similarly, the contents of the integer pointed to by constant_ptr can be modified, but constant_ptr itself will always point to the same location.

The declaration of the constant pointer constant_ptr can be clarified by including a definition for the type pointer to int . The following example declares constant_ptr as an object with type const-qualified pointer to int . The pointer's value (an address) is constant:


typedef int *int_ptr; 
const int_ptr constant_ptr; 

The __unaligned data-type qualifier can be used in pointer definitions on Alpha systems. to indicate to the compiler that the data pointed to is not properly aligned on a correct address. (To be properly aligned, the address of an object must be a multiple of the size of the type. For example, 2-byte objects must be aligned on even addresses.) (ALPHA)

When data is accessed through a pointer declared __unaligned , the compiler generates the additional code necessary to copy or store the data without causing alignment errors. It is best to avoid use of misaligned data altogether, but in some cases the usage may be justified by the need to access packed structures, or by other considerations. (ALPHA)

The __restrict data-type qualifier is used to designate a pointer as pointing to a distinct object, thus allowing compiler optimizations to be made (see Section 3.7.4).

Unless an extern or static pointer variable is explicitly initialized, it is initialized to a null pointer. A null pointer is a pointer value of 0. The contents of an uninitialized auto pointer are undefined.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement