How to use the printf() and scanf() functions to read and write values of different types

Programs work with data. You feed numbers, letters, and words to the computer, and you expect it to do something with the data. For example, you might want the computer to calculate an interest payment or display a sorted list of vintners. In this chapter, you do more than just read about data; you practice manipulating data, which is much more fun.

This chapter explores the two great families of data types: integer and floating point. C offers several varieties of these types. This chapter tells you what the types are, how to declare them, and how and when to use them. Also, you discover the differences between constants and variables, and as a bonus, your first interactive program is coming up shortly.

A Sample Program

Once again, you begin with a sample program. As before, you'll find some unfamiliar wrinkles that we'll soon iron out for you. The program's general intent should be clear, so try compiling and running the source code shown in Listing 3.1. To save time, you can omit typing the comments.

If you type this program incorrectly and, say, omit a semicolon, the compiler
gives you a syntax error message. Even if you type it correctly, however, the
compiler may give you a warning similar to "Warningconversion from
'double' to 'float,' possible loss of data." An error
message means you did something wrong and prevents the program from being
compiled. A warning, however, means you've done something that is
valid code but possibly is not what you meant to do. A warning does not stop
compilation. This particular warning pertains to how C handles values such as
770.0. It's not a problem for this example, and the chapter explains the
warning later.

When you type this program, you might want to change the 770.0 to
the current price of the precious metal rhodium. Don't, however, fiddle
with the 14.5833, which represents the number of ounces in a pound.
(That's ounces troy, used for precious metals, and pounds avoirdupois, used
for peopleprecious and otherwise.)

Note that "entering" your weight means to type your weight and then
press the Enter or Return key. (Don't just type your weight and wait.)
Pressing Enter informs the computer that you have finished typing your response.
The program expects you to enter a number, such as 150, not words, such
as too much. Entering letters rather than digits causes problems that
require an if statement (Chapter 7, "C Control Statements:
Branching and Jumps") to defeat, so please be polite and enter a number.
Here is some sample output:

Are you worth your weight in rhodium?
Let's check it out.
Please enter your weight in pounds: 150
Your weight in rhodium is worth $1684371.12.
You are easily worth that! If rhodium prices drop,
eat more to maintain your value.

What's New in This Program?

There are several new elements of C in this program:

Notice that the code uses a new kind of variable declaration. The
previous examples just used an integer variable type (int), but this
one adds a floating-point variable type (float) so that you can handle
a wider variety of data. The float type can hold numbers with decimal
points.

The program demonstrates some new ways of writing constants. You now have
numbers with decimal points.

To print this new kind of variable, use the %f specifier in the
printf() code to handle a floating-point value. Use the .2 modifier to
the %f specifier to fine-tune the appearance of the output so that it
displays two places to the right of the decimal.

To provide keyboard input to the program, use the scanf()
function. The %f instructs scanf() to read a floating-point
number from the keyboard, and the &weight tells scanf() to
assign the input value to the variable named weight. The
scanf() function uses the & notation to indicate where it
can find the weight variable. The next chapter discusses &
further; meanwhile, trust us that you need it here.

Perhaps the most outstanding new feature is that this program is
interactive. The computer asks you for information and then uses the number you
enter. An interactive program is more interesting to use than the noninteractive
types. More important, the interactive approach makes programs more flexible.
For example, the sample program can be used for any reasonable weight, not just
for 150 pounds. You don't have to rewrite the program every time you want
to try it on a new person. The scanf() and printf() functions
make this interactivity possible. The scanf() function reads data from
the keyboard and delivers that data to the program, and printf() reads
data from a program and delivers that data to your screen. Together, these two
functions enable you to establish a two-way communication with your computer
(see Figure 3.1), and that makes using a computer much more fun.

This chapter explains the first two items in this list of new features:
variables and constants of various data types. Chapter 4, "Character
Strings and Formatted Input/Output," covers the last three items, but this
chapter will continue to make limited use of scanf() and
printf().