The scanf Function

The information between the double quotes in the scanf function, is called a format string. It tells scanf what format the data coming from the user will take.

The spaces in the format string tell scanf to ignore any whitespace characters that the user might enter at those positions. Whitespace characters include ordinary spaces, tabs and carriage returns.

Each of the %d's specify that the user is expected to input a whole number in decimal, (the standard way people write numbers), at that position. Since they are separated by a comma which means nothing special to scanf, the user is actually expected to type a comma between the two whole numbers. Of course we could have omitted the comma and the user would only be expected to put some whitespace between the numbers.

Now it's no good if scanf allows-na the user to enter these two numbers and then just throws-na them away. They need to be stored somewhere, for later use in the program. The place to store such data, is in variables. This is what number1 and number2 are. The first line of our program proper defines these variables. Since they are going to contain whole numbers, they are declared to be of type int. Variables can have any name we choose, so long as they don't conflict with any internal C names (which are known as reserved words).

Since there are two %d's in our format string, scanf is given precisely two variables of type int to store the whole numbers that will come from the user.

The only peculiar thing here is that when the variables number1 and number2 are used in scanf, they are preceeded by ampersands. This is because the C specification requires that you pass not variables themselves to scanf, but pointers to those variables. The way you do this, is by putting the ampersands in front.

Essentially by supplying a pointer to the variables, scanf knows-na exactly where they are kept in memory and so can modify their contents. Without the ampersands, scanf would only be passed a copy of the contents of the variables, and just modifying a copy of the real thing, wouldn't be much use.

The easy way to remember this is that the ampersand stands for the address of the variable, which tells C where the actual variables are kept.