The scanf() Function -------------------- Just as the printf() function lets you output data, the scanf() function is a way to input data into a C program. The format of the command is as follows: scanf(string of qualifiers, list of addresses of variables); Lets look at some examples: scanf("%f", &num1); This inputs a floating point number into the variable "num1". Note: The address of "num1" is used. This is denoted by the "&". &num1 refers to the memory address of the variable called "num1". See section 3.2 if you are confused about memory addresses. Another example: scanf("%d %d %c", &a, &b, &f); In this case, 3 variables are assigned input values from one line of input. The first two are integers and the last is a character. By the way, the scanf() function comes from the same library as the printf() function. So "#include " is necessary in programs using it. Here is a sample program taking 2 integers as input: #include main() { int a1, a2; scanf("%d %d", &a1, &a2); printf("a1 = %d, a2 = %d\n",a1,a2); } The output is: % prog_2 1 23 a1 = 1, a2 = 23 Note: 1 23 was entered by the user from the keyboard. It is appropriate for the program to "ask" for input, otherwise the user will not know what to do. Here is an example of computing the sum of two numbers: #include main() { float z, y; float sum; printf("Enter two numbers: "); scanf("%f", &z); scanf("%f", &y); sum = z + y; printf("The sum is %f\n",sum); } Sample outputs are: %prog_3 Enter two numbers: 99 -89 The sum is 10.000000 %prog_8 Enter two numbers: 100 200 The sum is 300.000000 %prog_3 Enter two numbers: 1.3 -4.7 The sum is -3.400000