C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
+ number_of_notebooks*notebooks + lunchbox);
//The information for the second child
firstInitial = 'A';
middleInitial = 'J';
number_of_pencils = 10;
number_of_notebooks = 3;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial,number_of_pencils,
number_of_notebooks);
printf("The total cost is $%.2f\n\n", number_of_pencils*pencils
+ number_of_notebooks*notebooks + lunchbox);
//The information for the third child
firstInitial = 'M';
middleInitial = 'T';
number_of_pencils = 9;
number_of_notebooks = 2;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial,number_of_pencils,
number_of_notebooks);
printf("The total cost is $%.2f\n",
number_of_pencils*pencils + number_of_notebooks*notebooks +
lunchbox);
return 0;
}

This program gives examples of naming and defining different types of variables, as well as assigning
values to each. It’s important to note that you can reuse a variable by just assigning a new value to the
variable. You might be wondering, why keep using and reusing variables if you are just going to
change the value within the code itself? Why not just skip the variables and use the values in their
place? The value of variables will become more apparent after Chapter 8, “Interacting with Users,”
and you can get information from the user for these variables.


The Draw Poker program in Appendix B, “The Draw Poker Program,” must keep track of a lot of
things, and many variables are used there. At the start of most of the program’s functions, you’ll see a
place where variables are being defined.


Tip

You can define variables and give them initial values at the same time. The previous
program assigns values to the float variables pencil, notebook, and lunchbox
when they are declared.

The Absolute Minimum
Free download pdf