Home > Articles > Programming > C/C++

Data and the C Programming Language

  • Print
  • + Share This
  • 💬 Discuss

This chapter is from the book

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.

You will learn about the following in this chapter:

  • Keywords:

    int, short, long, unsigned, char, float, double, _Bool, _Complex, _Imaginary

  • Operator:

    sizeof

  • Function:

    scanf()

  • The basic data types that C uses
  • The distinctions between integer types and floating-point types
  • Writing constants and declaring variables of those types
  • 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, we 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 platinum.c Program

/* platinum.c  -- your weight in platinum */
#include <stdio.h>
int main(void)
{
    float weight;    /* user weight             */
    float value;     /* platinum equivalent     */

    printf("Are you worth your weight in platinum?\n");
    printf("Let's check it out.\n");
    printf("Please enter your weight in pounds: ");

    /* get input from the user                     */
    scanf("%f", &weight);
    /* assume platinum is $1700 per ounce          */
    /* 14.5833 converts pounds avd. to ounces troy */
    value = 1700.0 * weight * 14.5833;
    printf("Your weight in platinum is worth $%.2f.\n", value);
    printf("You are easily worth that! If platinum prices drop,\n");
    printf("eat more to maintain your value.\n");

    return 0;
}

When you type this program, you might want to change the 1700.0 to the current price of the precious metal platinum. 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 156, 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 platinum?
Let's check it out.
Please enter your weight in pounds: 156
Your weight in platinum is worth $3867491.25.
You are easily worth that! If platinum 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. The .2 modifier to the %f specifier fine-tunes the appearance of the output so that it displays two places to the right of the decimal.
  • The scanf() function provides keyboard input to the program. 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 156 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

    Figure 3.1 The scanf() and printf() functions at work.

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().

  • + Share This
  • 🔖 Save To Your Account
C Primer Plus, 6th Edition

This chapter is from the book

C Primer Plus, 6th Edition

Discussions

comments powered by Disqus