C – User Input with scanf()

Course Outline  | Previous LessonNext Lesson

Introduction – User Input

Programs written in C can collect data of all types like integer, float, and others from user input. Furthermore, the scanf function comes from the same library where printf comes from, which is stdio.h. Recall that the code below is the correct way to include a library to a program.

#include <stdio.h>

In addition, scanf is an integer function that returns the number of inputs that have successfully been read. More of that will be discussed later in this lesson.

Syntax of scanf()

Function scanf needs a variable to store data from user input. Before setting up scanf, software developers need to determine what kind of data they want to collect from users. In the example below, a software developer decides to collect an integer from users. Thus, the variable needs to be an int data type.

#include <stdio.h>

int main()
{
    int user_input;

    scanf("%d", &user_input);

    printf("The number is %d.\n", user_input);
   
    return 0;
}

The scanf function has two arguments. The first argument contains the placeholder, %d, that represents the type of data that scanf is expecting from users. When a user types something in via keyboard, he or she has to press enter to allow scanf to store the value into the user_input variable. In addition, it is required to have the symbol appended to the left of the variable. Do not worry about what that means in this lesson. Just remember to append the & symbol to the left of variables only with scanf.

If a user types in an integer 2 and press enter via keyboard, the output would be: The number is 2.

Multiple Variables for scanf()

Function scanf also accepts multiple variables at once. Please see the code below that stores two values.

#include <stdio.h>

int main()
{
    int user_input1, user_input2;

    scanf("%d %d", &user_input1, &user_input2);

    printf("First Number: %d \nSecond Number: %d\n", user_input1, user_input2);

    return 0;
}

Now there are two placeholders for the integers, which means that a user can store two values. When a user types in a number and presses enter, that number is stored to the first variable which is user_input1. When a user types in another number and presses enter, it is stored into the second variable which is user_input2. There is another way to do that from the user side. Users can type in the first number, press the space key, type in the second number, and press enter. The first and second number would be stored into the first and second variable respectively.

Changing Input Format

Function scanf can accept user input in a different format. Please refer to the code below for an example.

#include <stdio.h>

int main()
{
    int user_input1, user_input2;

    scanf("%d,%d", &user_input1, &user_input2);

    printf("First Number: %d \nSecond Number: %d\n", user_input1, user_input2);

    return 0;
}

In the first argument of the scanf function, “%d,%d”, there is a comma in between the placeholders. When a user types in 512,254, integers 512 and 254 would be stored into user_input1 and user_input2 respectively. If there was no comma in scan for the first argument, “%d%d”, and if a user types in 512,254, the second value would be stored incorrectly because the computer would not “know” what to do with the comma correctly. Thus, it is important for users to match the format of scanf. The code below is modified even further.

#include <stdio.h>

int main()
{
    int user_input1, user_input2;

    scanf("(%d, %d)", &user_input1, &user_input2);

    printf("First Number: %d \nSecond Number: %d\n", user_input1, user_input2);

    return 0;
}

Users would have to type something like this: (512, 254). Thus, the output with that input would be:

First Number: 512

Second Number: 254

The possibilities of formatting user inputs are virtually endless.

Multiple scanf()

Multiple scanf functions can be used to store multiple values individually. The program below asks for the person’s age and zip code.

#include <stdio.h>

int main()
{
    int age, zipCode;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your zip code: ");
    scanf("%d", &zipCode);

    printf("Age: %d, Zip Code: %d", age, zipCode);

    return 0;
}

Below is the sequence of inputs and outputs (output is in bold):

Enter your age: 23

Enter your zip code: 78705

Age: 23, Zip Code: 78705

The sequence above is what a user would experience when he or she uses that program.

Problem with char Data Type

There can be problems with multiple scanf functions that are designed to collect characters as inputs. Please refer to the code below for an example.

#include <stdio.h>

int main()
{
    char input1, input2;

    printf("Do you want to change the world for the better? (Y/N): ");
    scanf("%c", input1);

    printf("Are you a person with great integrity? (Y/N): ");
    scanf("%c", input2);

    printf("First Answer: %c \nSecond Answer: %c\n", input1, input2);

    return 0;
}

The code above looks correct, but it will not work correctly. When the user answers the first question, the program would skip the second scanf. The reason for that is when a user presses enter, it creates a newline character automatically, and that character is saved for the next scanf. Thus, the newline character is considered to be the second input, which is not correct. The following code below fixes that problem.

#include <stdio.h>

int main()
{
    char input1, input2;

    printf("Do you want to change the world for the better? (Y/N): ");
    scanf("%c", input1);

    fflush(stdin);

    printf("Are you a person with great integrity? (Y/N): ");
    scanf("%c", input2);

    printf("First Answer: %c \nSecond Answer: %c\n", input1, input2);

    return 0;
}

The function fflush clears all inputs (standard input – stdin). Thus, the newline character that was saved for the second scanf is deleted. In addition, the fflush is an integer function that returns 0 if it executes successfully. In the example above, fflush returns 0 since it successfully deletes the newline character. Standard input (stdin) is covered more in a future lesson since it is too complicated to go over in this lesson.

Below is the sequence of inputs and outputs (output is in bold):

Do you want to change the world for the better? (Y/N): Y

Are you a person with great integrity? (Y/N): Y

First Answer: Y

Second Answer: Y

The sequence above is what a user would experiences when he or she uses the program.

Function scanf with Other Data Types

The function scanf can collect data from all data types. Just like how printf can display data for all data types. The placeholders below can be used for scanf.

Please experiment more with scanf and the placeholders above in your code.

Return Value of scanf

Recall that scanf is an integer function that returns the number of inputs that are successfully read. Please refer to the code below as an example.

#include <stdio.h>

int main()
{
    int input1, input2, inputs;

    inputs = scanf("%d%d", &input1, &input2);

    return 0;
}

If a user enters two integers for the two variables, the inputs would match with the two decimal placeholders. Thus scanf would return an integer of 2. Basically, scanf would store that value to the inputs variable. In addition, if a user types in characters instead of integers for the program above, scanf would return a zero.

Conclusion

Function scanf is used to collect and store input from users, and it works like printf with placeholders and format. Multiple scanf functions that collect character data type would not work correctly due to a newline character that is automatically generated after a user presses enter. As a result, the newline character would be used as an input for the next scanf. Function fflush fixes that problem by deleting the newline character. That fflush function has to be used after scanf is used. Function scanf returns the number of inputs that are successfully read and matched with the placeholders within scanf.

Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz