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

106 Part II: Run and Scream from Variables and Math

Real, live constant variables

Using #define to create a constant for use throughout your program is a handy thing to do. In fact, I recommend using it whenever you have some­ thing in your program that you figure may change. For example:

#define NUMBER_OF_USERS 3 #define COLUMN_WIDTH 80 #define US_STATES 50

Each of these examples makes it possible to change aspects of your entire program merely by editing a single #define declaration. But are they really constants — the opposite of variables?

No! That’s because they have the const keyword, which converts a mildmannered variable into an unyielding constant. To wit:

const int senses = 6;

The preceding statement creates a variable named senses, but fixes that variable’s value at 6. The value cannot be changed or used for something else later in the program; if you try, a “read-only variable” error pops up.

Most often you don’t see const used to create constant values. Instead, this statement is quite common:

const char prompt[] = “Your command:”;

This statement creates the string variable prompt and sets its contents equal to Your command:. The const keyword ensures that this variable cannot be reused or its contents ever changed — a good idea for this type of variable.

Chapter 9

How to C Numbers

In This Chapter

Using different variables for different numbers

Understanding the long and short of int

Knowing your signed and unsigned variables

Floating a number

Double floating a number

Formatting a huge value

Put on your safety goggles, my greenhorn companion! I have danced around the flaming inferno of numbers far too long. It’s time to dive headlong into that hellfire of values and digits. Far down from the comfy safety of the int lie

numbers large and loathsome. Terrifying values you can gingerly place into your puny programs. Numbers lethal and toxic, but which you can also tame, as long as you obey my gentle advice in this chapter.

Fear not! Instead, don your asbestos suit and follow me. Watch your step.

There Are Numbers, and Then

There Are Numbers

Welcome to what will soon be one of many new, frustrating aspects of the C programming language. It’s known as the C Numeric Data Type Puzzle. Unlike in real life, where we can just pull any number out of the ethers and be joyously happy with it, in C you must pull numbers from specific parts of the ethers based on which type of number it is. This makes the frustration factor begin rising, with the logical question “What’s a number type?”

108 Part II: Run and Scream from Variables and Math

Okay. It isn’t a “number type.” It’s a numeric data type, which is how you say “number type” if you work at the Pentagon. You have to tell the C compiler which type of number you’re using because it thinks about numbers differently from the way humans do. For example, you have to know the following things about the number:

Is it a whole number — without a fraction or decimal part?

How big is the number (as in value-large, not big-on-the-page-large)?

If the number does have a fractional part, how precise must the number be? (Like to the thousandths, millionths, or gazillionths decimal place. Scientists have to know the precision when they send missiles to coun­ tries with opposing ideologies.)

I know that this stuff is all alien to you. What most programmers want to do is say “I need a number variable — just give me one, quick — before this value slips out the back of the computer and becomes a government statistic!” But you have to think a little more before you do that.

The most common numeric data type is the integer.

If you’re going to work with decimal numbers, such as a dollar amount, you need the floating-point number.

Keep reading.

Numbers in C

A number of different types of numbers are used in C — different numeric data types, so to speak. Table 9-1 lists them all, along with other statistical informa­ tion. Flag the table with a sticky note. This table is something you refer to now and again because only the truly insane would memorize it.

Table 9-1

C Numeric Data Types

Keyword

Variable Type

Range

char

Character (or string)

–128 to 127

 

 

 

int

Integer

–32,768 to 32,767

 

 

 

short

Short integer

–32,768 to 32,767

 

 

 

short int

Short integer

–32,768 to 32,767

 

 

 

 

 

 

Chapter 9: How to C Numbers

109

 

 

 

 

 

 

 

 

 

 

Keyword

Variable Type

Range

 

 

 

long

Long integer

–2,147,483,648 to 2,147,483,647

 

 

 

 

 

 

 

 

unsigned char

Unsigned character

0 to 255

 

 

 

 

 

 

 

 

unsigned int

Unsigned integer

0 to 65,535

 

 

 

 

 

 

 

 

unsigned short

Unsigned short integer

0 to 65,535

 

 

 

 

 

 

 

 

unsigned long

Unsigned long integer

0 to 4,294,967,295

 

 

 

 

 

 

 

 

float

Single-precision

±3.4 1038 to

 

 

 

floating-point

±3.4 10–38

 

 

 

(accurate to 7 digits)

 

 

 

 

double

Double-precision

±1.7 10–308 to

 

 

 

floating-point

±1.7 10308

 

(accurate to 15 digits)

The keyword is the C language keyword used to declare the variable type. If you have been reading the chapters in this book in order, you have used int, char, and float already.

The variable type tells you which type of variable the keyword defines. For example, char defines a character (or string) variable, and int defines integers. C has many variable types, each of which depends on the type of number or value being described.

The range tells you how big of a number fits into the variable type. For example, integers range from –32,768 up to 0 and up again to 32,767. Other types of variables handle larger values. This value may be differ­ ent on your compiler; use the values in Table 9-1 for reference only.

Table 9-1 isn’t that complex. In all, C has really only four types of variables:

char

int

float

double

The int can be modified with either short or long, and both char and int are modified with unsigned. The float and double variables are both floatingpoint, though the values held by double are larger.