The int Data Type
You saw the integer data type in Hour 3. The int keyword is used to specify the type of a variable as an integer. Integer numbers are also called whole numbers, which have no fractional part or decimal point. Therefore, the result of an integer division is truncated, simply because any fraction part is ignored.
Depending on the operating system and the C compiler you're using, the length of an integer varies. On most UNIX workstations, for example, an integer is 32 bits long, which means that the range of an integer is from 2147483647 (that is, 231_1) to -2147483648. The range of a 16-bit integer is from 32767 (that is, 215_1) to -32768.
The C compiler I'm using for this book is Visual C++ 1.5, which only provides the 16-bit integer, while a 32-bit version of Visual C++, such as Visual C++ 4.0 or Visual C++ 5.0, supports the 32-bit integer.
Declaring Integer Variables
You also saw the declaration of an integer in Hour 3. The following shows the basic declaration format:
int variablename;
Similar to the character declaration, if you have more than one variable to declare, you can use either the format like this
int variablename1;
int variablename2;
int variablename3;
or like this:
int variablename1, variablename2, variablename3;
Here variablename1, variablename2, and variablename3 indicate the places where you put the names of int variables.
Showing the Numeric Values of Characters
Like the character format specifier (%c) that is used to format a single character, %d, called the integer format specifier, is used to format an integer. You might recall that in line 16 of Listing 3.2, %d is used in the printf() function to format the second argument of the function to an integer.
In this section you're going to study a program, shown in Listing 4.3, that can print out the numeric values of characters by using the integer format specifier %d with printf().
TYPE
Listing 4.3. Showing the numeric values of characters.
1: /* 04L03.c: Showing the numeric values of characters */
2: #include <stdio.h>
3:
4: main()
5: {
6: char c1;
7: char c2;
8:
9: c1 = `A';
10: c2 = `a';
11: printf("The numeric value of A is: %d.\n", c1);
12: printf("The numeric value of a is: %d.\n", c2);
13: return 0;
14: }
OUTPUT
I get the following output on the screen after running the executable file, 04L03.exe:
C:\app> 04L03
The numeric value of A is: 65.
The numeric value of a is: 97.
C:\app>
ANALYSIS
You may find that the program in Listing 4.3 is quite similar to the one in Listing 4.1. As a matter of fact, I simply copied the source code from Listing 4.1 to Listing 4.3 and made changes in lines 11 and 12. The major change I made was to replace the character format specifier (%c) with the integer format specifier (%d).
The two statements in lines 11 and 12 format the two character variables (c1 and c2) by using the integer format specifier %d, and then print out two messages showing the numeric values 65 and 97 that represent, respectively, the characters A and a in the ASCII character set.