Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 8: Charting Unknown Cs with Variables

99

To have the output make more dollars and sense, edit Line 11 and change the %f placeholder to read %.2f:

printf(“That be $%.2f, please.\n”,price);

Squeezing extra characters between the % and the f should be familiar to you; I show you how to it a few chapters back, to limit the formatting for the %s placeholder. Here, you’re telling printf() to format the floating-point number to only two places after the decimal point.

Save the change to disk. Recompile and run. The output is more appealing:

Today special - Slimy Orange Stuff “Icky Woka Gu”

You want 1 pint.

That be $1.45, please.

This program contains three types of variables: a string, menuitem; an integer value, pints; and a floating-point value, price.

The price is a floating-point value because it contains a decimal part. It’s another type of numeric variable. Unlike an integer, floating-point values can contain a decimal part.

The “floating point” is that dot in the middle of the number — 1.45 — which is technically incorrect, but it’s the way I remember it.

Table 24-2 in Chapter 24 contains a list of the printf() function’s place­ holders. There, you find that %f is used to display a floating-point number, such as the one that appears in ICKYGU.C.

The final printf() statement is used to display the value of the floatingpoint price variable:

printf(“That be $%.2f, please.\n”,price);

To do that, you use the %f (f for float) placeholder. However, %f requires some extra formatting power to display the value as a monetary amount. To meet this end, you insert a “dot-2” between the % and the little f. That formats the output to only two decimal places. Rather than use %f, the formatting string uses %.2f.

Maybe you want to chance two pints?

You can easily twist ICKYGU.C into doing some math for you. Suppose that you want to figure out how much two pints of the orange stuff is. First, you change the pints variable in the sixth line to read

int pints=2;

100 Part II: Run and Scream from Variables and Math

That fills the pints variable with 2. Then you have to stick some math into the final printf() function, which calculates how much two pints of the sticky stuff would be. Make these alterations:

printf(“That be $%.2f, please.\n”,pints*price);

The only true change is in the last part of the line. Before, you had only the price variable. Now, you have pints*price, which multiplies the value in price by the value in pints. Because price is a floating-point, or decimal, value, the result still is floating-point. That is the reason that the %f place­ holder is still used in the formatting string.

Save these changes and recompile the program. You have to pay more, but — mmmm — your tummy will thank you.

Multiple declarations

C is full of abbreviations and shortcuts. That’s one reason that no two C pro­ grams look alike: Programmers always take advantage of the different ways of doing things. One such trick is to declare several variables in one statement. I know — this used to be illegal in parts of the South, but it’s now done above­ board everywhere in the Union.

The following three int statements create three integer variables: methus, you, and diff:

int methus; int you; int diff;

The following single-line statement does the same thing:

int methus,you,diff;

Each of the variables is specified after the int keyword and a space. Each is followed by a comma, with the final variable followed by a semicolon to end the statement.

This shortcut is primarily a space-saving technique. It just takes up less screen space to declare all variables of one type on a single line than to have individ­ ual, itsy-bitsy int statements lining up at the beginning of a program.

You can declare variables of only the same type in a multiple declaration. For example:

int top,bottom,right,left;

float national_debt,pi;