Sams Teach Yourself C in 24 Hours (16 page)

BOOK: Sams Teach Yourself C in 24 Hours
6Mb size Format: txt, pdf, ePub

06 067231861x CH04 4.10.2000 11:00 AM Page 61

Understanding Data Types and Keywords

61

Convert the value of c1 to character: A.

OUTPUT
Convert the value of c2 to character: a.

As you know, line 2 includes the header file, stdio.h, for the printf() function.

ANALYSIS
Lines 5–15 make up the main() function body.

Lines 6 and 7 declare two character variables, c1 and c2, while lines 9 and 10 assign c1

and c2 with the character constants ‘A’ and ‘a’, respectively.

Note that the %c format specifier is used in the printf() function in lines 11 and 12, which tells the computer that the contents contained by c1 and c2 should be printed as characters. When the two statements in lines 11 and 12 are executed, two characters are formatted and output to the screen, based on the numeric values contained by c1 and c2, respectively.

Now look at the program shown in Listing 4.2. This time, %c is used to convert the numeric values back to the corresponding characters.

TYPE

LISTING 4.2

Converting Numeric Values Back to Characters

1: /* 04L02.c: Converting numeric values back to characters */

2: #include

4

3:

4: main()

5: {

6: char c1;

7: char c2;

8:

9: c1 =65;

10: c2 =97;

11: printf(“The character that has the numeric value of 65 is: %c.\n”, c1); 12: printf(“The character that has the numeric value of 97 is: %c.\n”, c2); 13: return 0;

14: }

The following is the output printed on the screen of my computer after I run the executable file, 04L02.exe. (You might receive different output from your computer; it is implementation-dependent. That is, it’s up to the type of your computer, the operating system, and the C computer you’re using):

The character that has the numeric value of 65 is: A.

OUTPUT
The character that has the numeric value of 97 is: a

The program in Listing 4.2 is similar to the one in Listing 4.1 except for the two
ANALYSIS
statements in lines 9 and 10. Note that in lines 9 and 10 of Listing 4.2, the character variables c1 and c2 are assigned 65 and 97, respectively.

06 067231861x CH04 4.10.2000 11:00 AM Page 62

62

Hour 4

As you know, 65 is the numeric value (decimal) of the A character in the ASCII character set; 97 is the numeric value of a. In lines 11 and 12, the %c format specifier converts the numeric values, 65 and 97, into the A and a, respectively. The A and a characters are then printed out on the screen.

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.

Again, this can vary among different systems, so you can check the reference materials for your compiler to be sure.

Some C compilers, such as Visual C++ 1.5, provide only the 16-bit integer, whereas other 32-bit C compilers, such as Visual C++ 5.0, support 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 either use 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.

For example, the following statement declares MyInteger as an integer variable and assigns it a value:

int MyInteger = 2314;

06 067231861x CH04 4.10.2000 11:00 AM Page 63

Understanding Data Types and Keywords

63

Similarly, the following statement declares A, a, B, and b as integer variables: int A, a, B, b;

A = 37;

a = –37;

B = -2418;

b = 12 ;

You’ll learn more about the integer data type later in the book.

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 to 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 */

4

2: #include

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: }

I get the following output on the screen of my computer after running the executable file, 04L03.exe. (You may get a different output if your machine is not using the ASCII character set.)

The numeric value of A is: 65.

OUTPUT
The numeric value of a is: 97.

You may find that the program in Listing 4.3 is quite similar to the one in Listing
ANALYSIS
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).

06 067231861x CH04 4.10.2000 11:00 AM Page 64

64

Hour 4

Both format specifiers do basically the same thing — insert some data into the string you pass to printf() — but the difference is in the way printf() displays that data. The %c specifier always prints a character; the %d specifier always prints a number. Even when they refer to the exact same data, it will be printed the way you indicate in the format specifier regardless of the actual data type.

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.

The
float
Data Type

The
floating-point number
is another data type in the C language. Unlike an integer number, a floating-point number contains a decimal point. For instance, 7.01 is a floating-point number; so are 5.71 and –3.14. A floating-point number is also called a
real
number
.

A floating-point number is specified by the float keyword in the C language. Floating-pointer constants can be suffixed with f or F to specify float. A floating-pointer number without a suffix is double by default. The double data type is introduced later in this lesson.

Like an integer number, a floating-point number has a limited range. The ANSI standard requires that the range be at least plus or minus 1.0 × 1037. In most cases, a floating-point number is represented by taking 32 bits. Therefore, a floating-point number in C is of at least six digits of precision. That is, for a floating-point number, there are at least six digits (or decimal places) on the right side of the decimal point.

Unlike an integer division from which the result is truncated and the fraction is discarded, a floating-point division produces another floating-point number. A floating-point division is carried out if both the divisor and the dividend, or one of them, are floating-point numbers.

For instance, 571.2 / 10.0 produces another floating-point number, 57.12. So do 571.2

/ 10 and 5712 / 10.0.

Declaring Floating-Point Variables

The following shows the declaration format for a floating-point variable: float variablename;

06 067231861x CH04 4.10.2000 11:00 AM Page 65

Understanding Data Types and Keywords

65

Similar to the character or integer declaration, if you have more than one variable to declare, you can either use the format like this:

float variablename1;

float variablename2;

float variablename3;

or like the following one:

float variablename1, variablename2, variablename3;

For example, the following statement declares myFloat as a float variable and assigns it a value:

float myFloat = 3.14;

Similarly, the following statement declares a, b, and c as float variables: float a, b, c;

a = 10.38;

b = –32.7;

c = 12.0f;

The Floating-Point Format Specifier (
%f
)

4

You can also use the
floating-point format specifier
(%f) to format your output. Listing 4.4 shows an example of how to use the format specifier %f with the printf() function.

TYPE

LISTING 4.4

Printing the Results of Integer and Floating-Point Division

1: /* 04L04.c: Integer vs. floating-point divisions */

2: #include

3:

4: main()

5: {

6: int int_num1, int_num2, int_num3; /* Declare integer

variables */

7: float flt_num1, flt_num2, flt_num3; /* Declare floating

-point variables */

8:

9: int_num1 = 32 / 10; /* Both divisor and dividend are

integers */

10: flt_num1 = 32 / 10;

11: int_num2 = 32.0 / 10; /* The divisor is an integer */

12: flt_num2 = 32.0 / 10;

13: int_num3 = 32 / 10.0; /* The dividend is an integer */

14: flt_num3 = 32 / 10.0;

15:

16: printf(“The integer divis. of 32/10 is: %d\n”, int_num1);

continues

06 067231861x CH04 4.10.2000 11:00 AM Page 66

66

Hour 4

LISTING 4.4

continued

17: printf(“The floating-point divis. of 32/10 is: %f\n”, flt_num1); 18: printf(“The integer divis. of 32.0/10 is: %d\n”, int_num2);

19: printf(“The floating-point divis. of 32.0/10 is: %f\n”, flt_num2); 20: printf(“The integer divis. of 32/10.0 is: %d\n”, int_num3);

21: printf(“The floating-point divis. of 32/10.0 is: %f\n”, flt_num3); 22: return 0;

23: }

The following output is from the screen of my computer after the executable file, 04L04.exe, is run on my machine:

I did get several warning messages about type conversions while I was compiling the program in Listing 4.4, but I ignored them all because I wanted to create an executable file in order to show you the differences between the int data type and the float data type.

The integer divis. of 32/10 is: 3

OUTPUT

The floating-point divis. of 32/10 is: 3.000000

The integer divis. of 32.0/10 is: 3

The floating-point divis. of 32.0/10 is: 3.200000

The integer divis. of 32/10.0 is: 3

The floating-point divis. of 32/10.0 is: 3.200000

Inside the main() function, the two statements in lines 6 and 7 declare three inte-ANALYSIS ger variables, int_num1, int_num2, and int_num3, and three floating-point variables, flt_num1, flt_num2, and flt_num3.

Lines 9 and 10 assign the result of 32/10 to int_num1 and flt_num1, respectively; 32.0/10 to int_num2 and flt_num2 in lines 11 and 12, and 32/10.0 to int_num3 and flt_num3 in lines 13 and 14.

Then, lines 16–21 print out the values contained by the three int variables and the three floating-point variables. Note that %d is used for the integer variables, and the floating-point specifier (%f) is used for formatting the floating-point variables in the printf() function.

Because the truncation occurs in the integer division of 32/10, flt_num1 contains 3.000000, not 3.200000, which you can see from the second line of the output. However, flt_num2 and flt_num3 are assigned 3.200000 because both 32.0/10 and 32/10.0 are considered as the floating-point division.

06 067231861x CH04 4.10.2000 11:00 AM Page 67

Understanding Data Types and Keywords

67

But int_num2 and int_num3, as integer variables, discard respectively the fraction parts of the floating-point divisions of 32.0/10 and 32/10.0. Therefore, you just see the integer 3 in both the third and fifth lines of the output.

The
double
Data Type

In the C language, a floating-point number can also be represented by another data type, called the double
data type
. In other words, you can specify a variable by the double keyword, and assign the variable a floating-point number.

The difference between a double data type and a float data type is that the former uses twice as many bits as the latter. Therefore, a double floating-point number is of at least 10 digits of precision, although the ANSI standard does not specify it for the double data type.

In Hour 8, “Using Conditional Operators,” you’ll learn to use the sizeof operator to obtain the length in bytes of a data type, such as char, int, float, or double, specified on your computer system.

4

Using Scientific Notation

Other books

Shadow of the Wolf Tree by Joseph Heywood
Beware of Bad Boy by Brookshire, April
Benighted by Kit Whitfield
The Europe That Was by Geoffrey Household
Red Roses Mean Love by Jacquie D'Alessandro
Stalin's Gold by Mark Ellis
The Needle's Eye by Margaret Drabble