Skip to main content

Section 4.9 User Input Using scanf()

It is possible to have the user (the person sitting in front of the computer screen) modify the value of a variable while the program is running! To do so the user needs to enter a value (this is typically done by typing on the keyboard) and your program needs to be ready to accept such an input value and store it in a variable. The C-command to accomplish this is scanf().
The following code asks the user for their age, has them enter it and then prints it out. You’ll notice the strange ampersand (&) character in front of the variable in which we are storing the user input. We’ll explain this a bit later - for now: don’t worry.

Compile and run the code below in your own C environment.

#include <stdio.h>
int main(void) 
{
    int age;
    
    printf("How old are you? Please enter your age: ");
    scanf("%d", &age);
    printf("You are %d years old.\n", age);

    return(0);
}
Try it out by hitting the “Run” button! When prompted, enter your age.
You can read multiple entries with one scanf() statement if you wish (or you can simply use multiple scanf() statements in a row):

Compile and run the code below in your own C environment.

#include <stdio.h>
int main(void) 
{
    int num1;
    int num2;
    int num3;
    
    printf("Please enter three numbers:");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("You entered the following: \n%d\n%d\n%d\n", num1, num2, num3);
    
    return(0);
}
Let’s get back to our multiplication table. Rather than printing the x7 table, in this next example, we’ll have the user enter which multiplication table to print:

Compile and run the code below in your own C environment.

#include <stdio.h>
int main(void) 
{
    int i;
    int mult;
    
    printf("Which multiplication table would you like to print?\n");
    printf("Please enter a number: ");
    scanf("%d", &mult);
    printf("Here is the x%d multiplication table:\n", mult);
    
    for (i=1; i<=10; i++) {
        printf("%d x %d = %d\n", i, mult, i*mult);
    }
    
    return(0);
}

Activity 4.7. Adding User Input.

Please write a program that adds integers, entered by the user.
First, ask the user to enter the number of integers they wish to add. Next, use a for-loop that runs the desired number of times, and in which you continually ask the user to enter another number that then gets added to the sum.
When the loop terminates, print the result to the screen.
Here is a sample session, with simulated user input in bold:
How many numbers do you wish to add? 5
Please enter number 1: 7
Please enter number 2: 13
Please enter number 3: 4
Please enter number 4: -3
Please enter number 5: 9
The numbers you entered add up to 30.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void)
{
    int i; /* loop counter */
    int total; /* holds the sum */
    int num; /* individual numbers entered by the user */
    int count; /* hold number of numbers the user wishes to add */

    printf("How many numbers do you wish to add: ");
    scanf("%d", &count);

    /* Write your code here. */
    
    printf("The numbers you entered add up to %d.", total);
    return(0);
}