Kinds of Data
Your C programs must use data made up of numbers, characters, and words; programs process that data into meaningful information. Although many different kinds of data exist, the following three data types are by far the most common used in C programming:
- Characters
- Integers
- Floating points (also called real numbers)
Characters and C
A C character is any single character that your computer can represent. Your computer knows 256 different characters. Each of them is found in something called the ASCII table, located in Appendix A, “The ASCII Table.” (ASCII is pronounced askee. If you don’t know-ee, you can just ask-ee.) Anything your computer can represent can be a character. Any or all of the following can be considered characters:
A a 4 % Q ! + = ]
As you can see, every letter, number, and space is a character to C. Sure, a 4 looks like a number, and it sometimes is, but it is also a character. If you indicate that a particular 4 is a character, you can’t do math with it. If you indicate that another 4 is to be a number, you can do math with that 4. The same holds for the special symbols. The plus sign (+) is a character, but the plus sign also performs addition. (There I go, bringing math back into the conversation!)
All of C’s character data is enclosed in apostrophes ('). Some people call apostrophes single quotation marks. Apostrophes differentiate character data from other kinds of data, such as numbers and math symbols. For example, in a C program, all of the following are character data:
'A' 'a' '4' '%' ' ' '-'
None of the following can be character data because they have no apostrophes around them:
A a 4 % -
The first program in this chapter contains the character '\n'. At first, you might not think that \n is a single character, but it’s one of the few two-character combinations that C interprets as a single character. This will make more sense later.
If you need to specify more than one character (except for the special characters that you’ll learn, like the \n just described), enclose the characters in quotation marks ("). A group of multiple characters is called a string. The following is a C string:
“C is fun to learn.”
Numbers in C
Although you might not have thought about it before now, numbers take on many different sizes and shapes. Your C program must have a way to store numbers, no matter what the numbers look like. You must store numbers in numeric variables. Before you look at variables, a review of the kinds of numbers will help.
Whole numbers are called integers. Integers have no decimal points. (Remember this rule: Like most reality shows, integers have no point whatsoever.) Any number without a decimal point is an integer. All of the following are integers:
10 54 0 –121 –68 752
Numbers with decimal points are called floating-point numbers. All of the following are floating-point numbers:
547.43 0.0 0.44384 9.1923 –168.470 .22
The choice of using integers or floating-point numbers depends on the data your programs are working with. Some values (such as ages and quantities) need only integers; other values (such as money amounts or weights) need the exact amounts floating-point numbers can provide. Internally, C stores integers differently than floating-point values. As you can see from Figure 2.2, a floating-point value usually takes twice as much memory as an integer. Therefore, if you can get away with using integers, do so—save floating points for values that need the decimal point.
FIGURE 2.2. Storing floating-point values often takes more memory than integers.
Different C compilers use different amounts of storage for integers and floating-point values. As you will learn later, there are ways of finding out exactly how much memory your C compiler uses for each type of data.