In this chapter, students learn about the basic data types in C and about the distinctions between integer types and floating point types. The objectives important to this chapter include:
A program needs to work with data. Some data are preset before a program is used and are called constants. Another type of data, called variables, contains values that may be changed or assigned values as the program runs.
Besides variables and constants, data can have be of integer or floating point data type. An integer is a number with no fractional part. A floating point number can have a fractional part. In addition, a floating point number can represent a much larger range of values. The disadvantage is that floating point operations normally are slower than integer operations.
The data types associated with an integer are int, long, short, unsigned, and char (even though the last type is used to declare a character). The int type is a signed integer and has a range in values from -32768 to + 32767. An int variable is declared as follows:
Integer variables can also be declared and initialized using a single statement as in:
int hogs = 12;
Integer constants can be expressed in decimal (base 10), octal (base 8), and hexadecimal (base 16). The printf() used to print the number 100 using these three forms is:
printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
Other integer types are:
Type short requires less storage space than type long. The amount of space differs among computers; however, on many machines, type short takes a 2-byte unit while type long takes a 4-byte unit.
Type short often takes the same amount of space as type int.
The conversion specifiers for integers are as follows:
int %d /* also %i */ short %hd /* Sort of like half-d? */ long %ld unsigned %u
Finally, type char is used for storing characters such as letters and punctuation and is of integer type. Type char uses a format specifier of %c. It stores a numerical ASCII code rather than a letter of the alphabet. The ASCII code provides for 127 different types of letters, numbers, and special characters. Students should review this code shown in the Appendix.
A value of type char is declared as:
char ch; C contains several non-printing characters. Some of these are used to represent escape sequences, which are: \a Alert \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \\ Backslash \' Single quote \" Double quote \0oo Octal value /* \O followed by a number */ \xhh Hexadecimal value /* \x followed by a number */
Floating point numbers are of type float, type double, and long double. Type float provides for single precision and can store values with a range of 10e-37 to 10e+38 (expressed in exponential notation). Type double provides for double precision and typically uses twice as many bits as type float.
Floating point variables are declared as:
float noah; double trouble;
They can be initialized to represent floating-point constants.
The printf() function uses the conversion specifier %f to print type float and type double. It uses %e to display values in exponential notation.
When developing a program, type int and type float will be used most often. Even though C is forgiving about type mismatches, when you initialize a variable, match the constant type to the variable type, as in:
float oranges = 3.2;