C All-in-One Desk Reference For Dummies

One of the frustrating aspects of the C programming language is the C Numeric Data Type Puzzle. Unlike in real life, where you 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?"

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:

• Will it be a whole number — without a fraction or decimal part?
• How big will the number be (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 such precision when they send rockets to outer space to go where no one has gone before.)

Yes, this 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.

C uses a variety of number types — different numeric data types, so to speak. Table 1 lists them all, along with other statistical information. This table is something you'll refer to now and again because only the truly insane would memorize it all.

Table 1: C Numeric Data Types

 Keyword Variable Type Range Storage Required char character (or string) –128 to 127 1 byte int integer –32768 to 32,767 2 bytes short (or short int) short integer –32768 to 32,767 2 bytes long long integer –2,147,483,648 to 2,147,483,647 4 bytes unsigned char unsigned character 0 to 255 1 byte unsigned int unsigned integer 0 to 65,535 2 bytes unsigned short unsigned short integer 0 to 65,535 2 bytes unsigned long unsigned long integer 0 to 4,294,967,295 4 bytes float single-precision floating point (accurate to 7 digits) + or -3.4 x 1038 to + or -3.4 x10-38 4 bytes double double-precision floating point (accurate to 15 digits) + or -1.7 x 10-308 to + or -1.7 x10308 8 bytes
• The keyword is the C language keyword used to declare the variable type.
• The variable type tells you which type of variable the keyword defines. For example, char defines a character (or string) variable; int does integers; and so on. There are 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 will fit 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.
• The Storage Required column tells you how many bytes of storage each variable type requires. This is advanced stuff, not really necessary to know. Some computer scientists can look at the bytes required and proclaim, "Goodness! An integer on a PC occupies 16 bits of storage. That must explain the 32K range. Indeed. Hmmm. Pass the nachos."

Why use integers?

Obviously, if you have a double-precision floating-point number that can handle, essentially, numbers up to 1 gazillion, why bother with the puny little integer? Heck, make everything a double-whammy floating point and be done with it! Sounds good. Is bad.

Integers are truly the most common and handy types of numeric variables. Often, you need only small, whole-number values when you're programming. 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.

You have to concern yourself with only two types of integers: the normal integer — the int — and the long integer — the long.

The int is a whole-number value, 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. In all DOS C compilers, it's just called int. (It rhymes with bent, not pint.)

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. With DOS C compilers, you can freely mince about, calling it just long.

In continuance with humankind's obsession with size, it would seem obvious — nay, greedy — to always want to use the long over the int. After all, bigger is better. Although that may be true, and psychologists can debate why most people feel that way, the truth is that the smaller the variable type you can get away with, the quicker your program runs. The int variables are tiny and tidy, easy for the computer to figure on its two thumbs. long variables require a little more time to compute, and it wastes the computer's memory and processing power to use them when you're better off with ints. (You'll see why this is so as you continue to program in C.)

• You use the int and long keywords to declare integer variables. int is for smaller values; long is for larger values.
• The %i placeholder is used in the printf function to display int variables. (You can also use the %d placeholder.)
• int = short = short 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 int s whenever you need a small, whole numeric value.
• Negative numbers — why bother? Sometimes you need them, but most of the time you don't.
• 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).