Previous: Defining a Function --- main()
Up: 2.2 Organization of C Programs - Simple Statements
Next: 2.2.4 The Assignment Statement
Previous Page: Defining a Function --- main()
Next Page: 2.2.4 The Assignment Statement

2.2.3 Variable Declarations

A variable is a language construct for identifying the data items used within a block. The declaration statements give names to these data items and specify the type of the item. The first two statements in our program are such declarations. The information we have in our task is the employee ID, the number of hours worked by the employee and the rate of pay. In addition, we will compute the total amount of pay for the employee and must declare a variable for this information. We have named variables for this information: id_number, hours_worked, rate_of_pay, and pay. We have also specified the type of each; for example, id_number is a whole number which requires an integer type, so the keyword int is used. The remaining data items are real numbers (they can have fractional values), so the keyword float is used to specify floating point type.

Variables of appropriate type ( int, float, etc.) must be declared at the head of the block in which they are used. Several variables of the same type may be grouped together in a declaration, separated by commas.

int id_number;
     float hours_worked,
           rate_of_pay,
           pay;

The names we have chosen for the variables are somewhat arbitrary; however, to make programs readable and easier to understand, variable names should be descriptive and have some meaning to the programmer. In programming languages, names are called identifiers and must satisfy certain rules.

First, identifiers may not be keywords (such as int and float) which have special meaning in C and are therefore reserved. All of these reserved words are listed in Appendix . Otherwise, identifiers may include any sequence of lower and upper case letters, digits, and underscores; but the first character must be a letter or an underscore (though the use of an underscore as a first character is discouraged). Examples of legal identifiers include PAD12, pad39, room_480, etc. Alphabetic letters may be either lower case or upper case which are different; i.e. PAY, Pay, and pay are distinct identifiers for three different objects. There is no limit to the length of an identifier, however, there may be an implementation dependent limit to the number of significant characters that can be recognized by a compiler. (This means that if two identifiers do not differ in their first characters, the compiler will not recognize them as distinct identifiers. A typical value for might be 31).

The general form for a declaration statement is:

Throughout this text we will be presenting syntax specifications as shown above. The items surrounded by angle brackets () are constructs of the language, for example <type_specifier> is a type specifier such as int or float, and <identifier> is a legal identifier. Items surrounded by square brackets ( [ ]) are optional, i.e. they may or may not appear in a legal statement. The ellipsis (...) indicates one or more repetitions of the preceding item. Any other symbols are included in the statement exactly as typed. So, in words, the above syntax specification says that a declaration statement consists of a type specifier followed by an identifier and, optionally, one or more other identifiers separated by commas, all terminated by a semicolon.

As for the semantics (meaning) of this statement, a declaration statement does two things: allocates memory within the block for a data item of the indicated type, and assigns a name to the location. As we saw in Chapter , data is stored in the computer in a binary form, and different types of data require different amounts of memory. Allocating memory for a data item means to reserve the correct number of bytes in the memory for that type, i.e. choosing the address of the memory cells where the data item is to be stored.

Figure 2.2 shows memory allocation for the declarations in our program as it might occur on a 16 bit machine. The outer box shows that these variables have been allocated for the function main(). For each variable we show the size of the data item (in bytes), its type, name and assigned address assignment (in hex) above the box representing the cell itself. In the future, we will generally show only the memory cell and its name in similar diagrams. Note that the declaration statements do not put values in the allocated cells. We indicate this with the ?? in the boxes.

Memory cells allocated for specific data types are called objects. An object is identified by its starting address and its type. The type determines the size of the object in bytes and the encoding used to represent it. A variable is simply a named object which can be accessed by using its name. An analogy is gaining access to a house identified by the name of the person living there: Smith house, Anderson house, etc.

Memory is automatically allocated for variables declared in a block when the block is entered during execution, and the memory is freed when the block is exited. Such variables are called automatic variables. The scope of automatic variables, i.e. the part of a program during which they can be used directly by name, is the block in which they are defined.



Previous: Defining a Function --- main()
Up: 2.2 Organization of C Programs - Simple Statements
Next: 2.2.4 The Assignment Statement
Previous Page: Defining a Function --- main()
Next Page: 2.2.4 The Assignment Statement

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994