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 DEC C documentation for specific information on the range and precision of floating-point types.


Previous Page | Next Page | Table of Contents | Index