Variables and Assignment
Few programs are as simple as the one in Most programs need to perform a series of calculations before producing output, and thus need a way to store data temporarily during program execution. In C, as in most programming languages, these storage locations are called variables.
Every variable must have a type, which specifies what kind of data it will hold. C has a wide variety of types. For now, we'll limit ourselves to just two: int and float. Choosing the proper type is critical, since the type affects how the variable is stored and what operations can be performed on the variable. The type of a numeric variable determines the largest and smallest numbers that the variable can store; it also determines whether or not digits are allowed after the decimal point.
A variable of type int (short for integer) can store a whole number such as 0, I, 392, or -2553. The range of possible values is limited, though. The largest int value is typically 2,147.483.647 but can be as small as 32.767.
A variable of type float (short for floating-point) can store much larger numbers than an int variable. Furthermore, a float variable can store numbers with digits after the decimal point, like 379.125. float variables have drawbacks, however. Arithmetic on float numbers may be slower than arithmetic on int numbers. Most significantly, the value of a float variable is often just an approximation of the number that was stored in it. If we store 0.1 in a float variable, we may later find that the variable has a value such as 0.09999999999999987, thanks to rounding error.
Variables must be declared described for the benefit of the compiler before they can be used. To declare a variable, we first specify the type of the variable, then its name. (Variable names are chosen by the programmer, we might declare variables height and profit as follows:
The first declaration states that height is a variable of type int. meaning that height can store an integer value. The second declaration says that profit is a variable of type float.
If several variables have the same type, their declarations can be combined:
int height, length, width, volume; float profit, loss;
Notice that each complete declaration ends with a semicolon.
Our first template for main didn't include declarations. When main contains declarations, these must precede statements:
This is true of functions in general, as well as blocks (statements that contain embedded declarations). As a matter of style, it's a good idea to leave a blank line between the declarations and the statements.
In C99, declarations don't have to come before statements. For example, main might contain a declaration, then a statement, and then another declaration. For compatibility with older compilers, the programs in this book don't take advantage of this rule. However, it's common in C++ and Java programs not to declare variables until they're first needed, so this practice can be expected to become popular in C99 programs as well.
A variable can be given a value by means of assignment. For example, the statements
height = 8;
length = 12;
width = 10;
assign values to height, length, and width. The numbers 8. 12. and 10 are said to be constants.
Before a variable can be assigned a value or used in any other way, for that matter it must first be declared. Thus, we could write
int height; height = 8;
height = 8; /*** WRONG ***/ int height;
A constant assigned to a float variable usually contains a decimal point. For example, if profit is a float variable, we might write
profit = 2150.48;
It's best to append the letter f (for “float”) to a constant that contains a decimal point if the number is assigned to a float variable:
profit = 2150.48f;
Failing to include the f may cause a warning from the compiler.
An int variable is normally assigned a value of type int, and a float variable is normally assigned a value of type float. Mixing types (such as assigning an int value to a float variable or assigning a float value to an int variable) is possible but not always safe.
Once a variable has been assigned a value, it can be used to help compute the value of another variable:
height = 8; length.= 12; width=10;
volume = height * length * width; /* volume is now 960 */
In C. * represents the multiplication operator, so this statement multiplies the values stored in height, length, and width, then assigns the result to the variable volume. In general, the right side of an assignment can be a formula (or expression, in C terminology) involving constants, variables, and operators.
Printing the Value of a Variable
We can use printf to display the current value of a variable. For example, to write the message
where h is the current value of the height variable, we'd use the following call of printf:
printf(“Height: %d\n”, height);
%d is a placeholder indicating where the value of height is to be filled in during printing. Note the placement of \n just after %d, so that printf will advance to the next line after printing the value of height.
%d works only for int variables; to print a float variable, we'd use %f instead. By default, %f displays a number with six digits after the decimal point. To force %f to display p digits after the decimal point, we can put .p between % and f.