Click here to view code image
price = 8.50 * .65; // Gets price after 35% discount
You can even use other variables in the expression:
Click here to view code image
totalAmount = price * quantity; /* Uses value from another variable
*/
Tip
The equals sign tells C this: Take whatever is on the right and stick it into the variable
on the left. The equals sign kind of acts like a left-pointing arrow that says “That-a-
way!” Oh, and never use commas in numbers, no matter how big the numbers are!
Let’s use all this variable knowledge you’ve gained in a program. Open your editor, type the
following program, compile it, and run it:
Click here to view code image
// Example program #1 from Chapter 5 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter5ex1.c
/* This is a sample program that lists three kids and their school
supply needs, as well as cost to buy the supplies */
#include <stdio.h>
main()
{
// Set up the variables, as well as define a few
char firstInitial, middleInitial;
int number_of_pencils;
int number_of_notebooks;
float pencils = 0.23;
float notebooks = 2.89;
float lunchbox = 4.99;
//The information for the first child
firstInitial = 'J';
middleInitial = 'R';
number_of_pencils = 7;
number_of_notebooks = 4;
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