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

110 Part II: Run and Scream from Variables and Math

Why use integers? Why not just make every number floating-point?

Obviously, if you have a double-precision floating-point number that can handle, essentially, numbers up to one gazillion, why bother declaring any variables as integers? Heck, make everything a double-whammy floating point and be done with it! Sounds good. Is bad.

Integers are truly the handiest and most common types of numeric variables. Oftentimes, you need only small, whole-number values when you’re program­ ming. Floating-point numbers are okay, but they require more overhead from the computer and take longer to work with. By comparison, integers are far quicker. For this reason, God saw fit to create integers (which He did on the third day, by the way).

Integer types (short, long, wide, fat, and so on)

You have to concern yourself with only two types of integers: the normal integer — the int — and the long integer — the long. (The signed and unsigned aspects are chewed over slowly later in this chapter.)

The int (rhymes with “bent”) is a whole-number value, normally ranging from –32,768 to 32,767. It’s ideally put to use for small numbers without a fractional part. In some versions of C, you may see this value referred to as a short or short int.

The long is a whole-number value, ranging from –2,147,483,648 to 2,147,483,647 — a big range, but not big enough to encompass the national debt or Madonna’s ego. This type of numeric variable is referred to as a long, or long int in some versions of C.

You use the int and long keywords to declare integer variables. int is for smaller values; long is for larger values.

The %d placeholder is used in the printf() function to display int vari­ ables. (You can also use the %i placeholder; refer to Table 24-2 in Chap­ ter 24.)

int = short = short int

long = long int

Integer variables (int) are shorter, faster, and easier for the computer to deal with. If Soup for One were a variable, it would be an int. Use an int whenever you need a small, whole numeric value.

Chapter 9: How to C Numbers 111

In some C compilers, the ranges for int and long int are the same. That’s because the compiler (usually a 32-bit model) can more efficiently handle long values than it can handle smaller int values. It’s merely technical junk; don’t memorize it or let it otherwise ruin your day.

Negative numbers — why bother? Sometimes, you need them, but most of the time you don’t. See the next section.

The char variable type can also be used as a type of integer, though it has an extremely small range. These variables are used mostly to store single characters (or strings), which is discussed somewhere else. (Give me a second to look.) Oh, it’s in Chapter 10.

Signed or unsigned, or “Would you like a minus sign with that, Sir?”

I have this thing against negative numbers. They’re good only when you play Hearts. Even so, that’s justification because you may someday write a program that plays Hearts on the computer, in which case you will be in dire need of negative numbers (because you can write the program so that you always win).

When you declare a numeric variable in C, you have two choices: signed and unsigned. Everything is signed unless you specifically type unsigned before the variable type:

unsigned int shoot_the_moon = 26;

A signed type of number means that a variable can hold a negative value. The standard int variable can hold values from –32,768 up to 32,767. That’s half negative numbers, from –32,786 to –1, and then half positive numbers, from 0 up to 32,767. (Zero is considered positive in some cults.)

An unsigned number means that the variable holds only positive values. This unsigned number moves the number range all up to the positive side — no negatives (the C language equivalent of Prozac). Your typical unsigned int has a range from 0 to 65,535. Negative numbers aren’t allowed.

The int variable elephants holds the value 40,000. Try that with a signed int! Ha!

unsigned int elephants 40000;

Table 9-2 illustrates the differences between the variable types as far as the values they can hold are concerned.

112 Part II: Run and Scream from Variables and Math

The whole painful spiel on why we have signed integers

The signed-unsigned business all has to do with how numbers are stored inside a computer. The secret is that everything, no matter how it looks on the screen or in your program, is stored in the binary tongue inside the computer. That’s counting in base 2 (ones and zeroes).

Binary numbers are composed of bits, or binary digits. Suppose that your C language compiler uses two bytes to store an integer. Those two bytes contain 16 binary digits, or bits. (Eight bits are in a byte.) For example:

0111 0010 1100 0100

This value is written as 29,380 in decimal (the human counting system). In binary, the ones and zeroes represent various multiples of two, which can get quite complex before your eyes, but is like eating ice cream to the computer.

Look at this number:

0111 1111 1111 1111

It’s the value 32,767 — almost a solid bank of ones. If you add 1 to this value, you get the fol­ lowing amazing figure:

1000 0000 0000 0000

How the computer interprets this binary value depends on how you define your variable. For a signed value, a 1 in the far left position of the number isn’t a 1 at all. It’s a minus sign. The pre­ ceding number becomes –32,768 in binary math. If the variable is an unsigned value, it’s inter­ preted as positive 32,768.

The deal with signed and unsigned numbers all depends on that pesky first bit in the computer’s binary counting tongue. If you’re working with a signed number, the first bit is the minus sign. Otherwise, the first bit is just another droll bit in the computer, happy to be a value and not a minus sign.

Table 9-2 What Signed and Unsigned Variables Can Hold

Signed

Range

Unsigned

Range

char

–128 to 127

unsigned char

0 to 255

 

 

 

 

int

–32768 to 32,767

unsigned int

0 to 65,535

 

 

 

 

long

–2,147,483,648

unsigned long

0 to 4,294,967,295 to 2,147,483,647

 

 

 

 

Floating-point numbers (numbers with a decimal part or fractions) can be positive or negative without regard to any signed or unsigned nonsense.

Floating-point numbers are covered in the following section.

Normally, the differences between signed and unsigned values shouldn’t bother you.