The library function scanf() performs several tasks. It determines which keys the user has pressed, calculates, in the computer's internal form, the number that the user has typed and finally stores the number in a particular memory location.
In many elementary programs scanf() will be used to read from the keyboard. In fact scanf() reads from something called the standard input , this is usually the keyboard but the host operating system has complete freedom to connect something else to the standard input. This may, typically, be a stream of data read from a file.
The simple examples of the use of scanf() in the sum of two numbers program each have two parameters. If a function is called with more than one parameter then the parameters are supplied as a comma separated list.
The first parameter is a string of the sort we have already seen used with printf() as will be seen from the enclosing double quotes. The purpose of the string is to tell scanf() what rules to apply when converting from the set of keys pressed by the user to the internal form of number storage used by the computer. This is necessary because when you type a number such as 27 on the keyboard this causes the two characters "2" and "7" to be sent to the computer. Internally computers store numbers using the binary system, which is convenient for electronic manipulation but totally unsuitable for human use. The library function scanf() will determine that the keys pressed were "2" and "7" and use this information to generate the internal binary number 0000000000011011. (The number of zeroes at the front varies from computer to computer.)
Within the string %d means convert from external decimal form to an internal binary integer. External decimal form is simply the way you normally write or type integers. There are many other forms of conversion that will be described later. The initial percent symbol ( % ) in the string means that the following "d" is a conversion type specification rather than specifying that scanf() is expecting to find a d in the input.
The second parameter is the address of the place to store the number read in and converted. The ampersand (&) means "address of" and must not be left out. Leaving out the ampersand almost always results in chaos. If you want to see what sort of chaos results skip on towards the end of this chapter.