Sams Teach Yourself C in 21 Days

(singke) #1
Storing Information: Variables and Constants 49

3


typenamespecifies the variable type and must be one of the keywords listed in Table 3.2.
varnameis the variable name, which must follow the rules mentioned earlier. You can
declare multiple variables of the same type on one line by separating the variable names
with commas:
int count, number, start; /* three integer variables */
float percent, total; /* two float variables */
On Day 12, “Understanding Variable Scope,” you’ll learn that the location of variable
declarations in the source code is important, because it affects the ways in which your
program can use the variables. For now, you can place all the variable declarations
together just before the start of the main()function.

ThetypedefKeyword ....................................................................................


Thetypedefkeyword is used to create a new name for an existing data type. In effect,
typedefcreates a synonym. For example, the statement
typedef int integer;
createsintegeras a synonym for int. You then can use integerto define variables of
typeint, as in this example:
integer count;
Note that typedefdoesn’t create a new data type; it only lets you use a different name
for a predefined data type. The most common use of typedefconcerns aggregate data
types, as explained on Day 11, “Structures.” An aggregate data type consists of a combi-
nation of data types presented today.

Initializing Variables ........................................................................................


When you declare a variable, you instruct the compiler to set aside storage space for the
variable. However, the value stored in that space—the value of the variable—isn’t
defined. It might be zero, or it might be some random “garbage” value. Before using a
variable, you should always initialize it to a known value. You can do this independently
of the variable declaration by using an assignment statement, as in this example:
int count; /* Set aside storage space for count */
count = 0; /* Store 0 in count */
Note that this statement uses the equal sign (=), which is C’s assignment operator and is
discussed further on Day 4, “Statements, Expressions, and Operators.” For now, you need
to be aware that the equal sign in programming is not the same as the equal sign in alge-
bra. If you write
x = 12

06 448201x-CH03 8/13/02 11:14 AM Page 49

Free download pdf