#include <stdio.h>
void main()
{
int numcandy; // declare a number variable
double cost; // declare a variable that can store decimals
printf("How many lollipops do you want: ");
scanf("%d", &numcandy); // get input from user
cost = 0.55 * numcandy; // do some math
printf("\nPlease pay %f to the cashier!\n", cost);
}
This program when ran, will prompt the user for how many lollipops they would like. After
they enter a number and press enter, it will print out how much it will cost them to buy
that many lollipops assuming lollipops cost 55 cents each.
The main, {} brackets, comments, and include should be familiar. The new part of this
program is the scanf, variables, and the use of printf to print out numbers are the parts of this program that we haven't seen before.
First lets talk about the variables. This program
has two variables numcandy, and cost. These are the names of the variables. Before a variable can
be used it must be declared. Declaring a variable in C is easy, you simply tell the compiler the
type, and the name you want the variable to have. So in the above example. The line int numcandy; tells the
compiler you want a variable of type integer with the name numcandy. Then you can simply use numcandy through out your
program to store int's (integers). Integers are non decimal numbers like -13, 0, 10, 5, etc. The other variable in
the above program is a double which is used for storing numbers with decimals.
Next the scanf, and printf statements. After running the program you will notice that the scanf function
simply gets a value from the user. It waits for the user to enter a number, and then press enter. It puts
this value into our variable numcandy. Next notice some special character sequences contained in
both the scanf and printf. First the printf has the sequence "\n". This sequence simply means move to the
next line. Next we have the %f and %d. These tell printf and scanf what type of variables to expect. %f corresponds
to double, and %d is for int. So if you changed the above programs printf line to ...pay $%d to... then you would get
wierd output since printf would try to print cost as an integer, which does not make sense, because cost is a double.
You need to make sure the "%" go with the right types.
Variable names are arbitrary (with some compiler-defined maximum length,
typically 32 characters). C uses the following standard variable types: int -> integer variable
short -> short integer
long -> long integer
float -> single precision real (floating point) variable
double -> double precision real (floating point) variable
char -> character variable (single byte)
The printf function can be instructed to print integers, floats
and strings properly. The general syntax is printf( "format", variables );
where "format" specifies the converstion specification and
variables is a list of quantities to print. Some useful formats are
%.nd integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf float or double (optional m = number of columns,
n = number of decimal places)
%ns string (optional n = number of columns)
%c character
\n \t to introduce new line or tab
So using this new knowledge, you could change the above program where it prints the cost to ...pay $.2f to... which
would print the number, and then only two decimal places of the double. Look back up at the program one more time at the line
cost = 0.55 * numcandy;. This is the math of the program. This line is fairly self explanatory. It takes the value
which is stored in numcandy multiplies it with 0.55 and puts the resulting value into cost. Here are a list of the standard
math operators you can use in your programs:+ add
- subtract
* multiply
/ divide
= equals (assignment operator)
|