scanf: The C Programming Language §7.4 pp 128-130

Function scanf

Some of the programs below need to get a number from the user. So before getting to the problems we discusss how to get a number from the user.


Getting input numbers: the function scanf

There are two parts to getting a number from the user.

First we need to set up a place for the user's number to be stored. That's easy. For these problems the user's number will be an integer. So we will write in function main:

   int num;
to set up an integer variable. You get your choice for the name.

Second: scanf. The function scanf gets user input (what he or she types). If the user has already typed something, scanf uses that, otherwise scanf stops and waits for the user to type a bunch of keys and press the enter key. Function scanf expects at least two arguments.

The first argument is a conversion specification. We will be using "%d" since the user will be typing decimal digits possibly preceded by a sign (+ or -). Once the user presses the enter key, scanf scans the input characters. Initial whitespace is ignored. Whitespace is characters like spaces, tabs, and newlines. Non-whitespace is everything else. scanf takes as much (digits) as it can. It stops when it hits a non-digit.

The second argument is an address; the address where we want to store the (converted) user's input. The ampersand (&) is the address operator. Thus &num is the address of variable num. So we write:

   int num;
   scanf("%d", &num);
Example. Below we have a complete program. When we run it, the first printf prints the first line. The purpose of the first printf is to tell the user what he or she is supposed to do. The next statement is scanf. scanf waits for the user to enter something. Now suppose what the user types is the second line below and then he or she presses the enter key.
Please type a number, some spaces, a second number, then press enter.
       -22  33
The scanf will skip the initial spaces. It will pick up the characters -22 and stop when it hits the space after the 22. It will convert the characters into an integer and finally store the integer into variable num.
#include<stdio.h>
int main() {
    int num =0;
    printf("Please type a number, some spaces, a second number, then press enter.";
    scanf("%d", &num);
    printf("first=%d\n", num);
    scanf("%d", &num);
    printf("second=%d\n", num)
}
The second printf will print
first=-22
The second scanf will pick up scanning the input characters from where the first scanf left off. It will skip the spaces and pick up the characters 33, stopping on the newline (not visible) at the end. It will convert the characters into an integer and store the integer into num. So the third printf will print:
second=33

Big Data

Consider the program:
#include<stdio.h>
int main () {
  int c = 0;
  int num;
  while (1 == scanf("%d", &num) )
    c += 1;
  printf("%d\n", c);
}
The function scanf tries to get a number from input and store it in variable num. Each time it is successful with getting a number and storing it in num it returns the number 1. That is, in the expression
  1 == scanf("%d", &num) 
the value of scanf("%d", &num) is 1. It returns 1 because it has gotten 1 more input number! Thus each time scanf is successful the expression above simplifies to:
  1 == 1
which is true so the loop repeats: variable c becomes one larger and the condition is re-checked so scanf is run one more time. So long as scanf finds only white space and integers written with decimal digits the loop continues until the numbers run out (EOF: End Of File). Then scanf returns -1 and the condition after the while simplifies to
  1 == -1
which is false. The loop ends, we go on to the next statement that prints out the number of numbers in the file.

IMPORTANT: There are two ways to run this program. First you must write, save (make up a name, say, bd.c), and compile it.

One way to run it is with input from the keyboard. Enter the command:

   a.out
(Remember to press the enter key). Now enter several integers. Each time you press enter, the program processes the numbers you entered. Now hold down the control/ctrl key and type a D. This is the EOF marker in unix. The program should now print out the number of integers in its input.

A second way to run it is have the program use a file of integers as input. Make a file of integers. Use pico to write some integers and save them in a file (say, give it the name data). Then enter the command:

   a.out < data
The "< data" tells the operating system to take its input from the file data. The program should print out the number of numbers in data.

next: Planning Progams