A Sample Program

I l @ ve RuBoard

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.

Listing 3.1 The goldyou.c program.
 /* goldyou.c  -- the worth of your weight in gold */ #include <stdio.h> int main(void) {     float weight;    /* user weight            */     float value;     /* user's gold equivalent */     printf("Are you worth your weight in gold?\n");     printf("Let's check it out.\n");     printf("Please enter your weight in pounds: "); /* get input from the user                     */     scanf("%f", &weight); /* assume gold is 0 per ounce               */ /* 14.5833 converts pounds avd. to ounces troy */     value = 320.0 * weight * 14.5833;     printf("Your weight in gold is worth $%.2f.\n", value);     printf("You are easily worth that! If gold prices drop,\n");     printf("eat more to maintain your value.\n");     return 0; } 

Errors and Warnings

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 this: "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 like 320.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 320.0 to the current price of gold. I suggest, however, that you don't 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. Here is a sample output:

 Are you worth your weight in gold? Let's check it out. Please enter your weight in pounds: 170 Your weight in gold is worth 3331.52. You are easily worth that! If gold 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 you use a new kind of variable declaration. Previously, you used only an integer variable type ( int ), but now you've added 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 type in. An interactive program is more interesting to use than the noninteractive types. More important, the interactive approach makes programs more flexible. For instance, the sample program can be used for any reasonable weight, not just for 170 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.

    Figure 3.1. The functions scanf() and printf() at work.
    graphics/03fig01.jpg

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 you continue to make limited use of scanf() and printf() in this chapter.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net