?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    scanf() Function and its Use in C programming

    In this lesson, we will look at what is scanf() function in C programming and how it works along with some example.

    What is scanf() Function

    The scanf() function in C is a standard library function that is part of the standard input-output library (stdio.h). The scanf() function allows us to read input from the standard input device, such as the keyboard, and store the data in variables of various data types, including integers, floating-point numbers, characters, and strings.

    Video Explanation

    Click the button below to watch the explanation.

    video image
    Syntax of scanf()
    scanf("format", &variable, &variable, ...);
    • format: This is a string that tells scanf what type of data need to be input from the keyboard. It uses special codes called format specifiers (like %d for an integer, %f for a float, %lf for double, and %c for a character).
    • &variable, &variable, ...: These are the variables where the data will be stored which are input from the keyboard. We need to give scanf the addresses of these variables using the & symbol at the beginning of each variable.

    Common Format Specifiers used in scanf()

    • %d: Used for reading an integer datatype value from the keyboard.
    • %f: Used for reading a float datatype value from the keyboard.
    • %lf: Used for reading a double datatype value from the keyboard.
    • %c: Used for reading a character datatype value from the keyboard.

    Let's see an example of how to use scanf to read different types of data from the keyboard using the format specifiers.

    Example
    #include <stdio.h>
    
    int main()
    {
        int age;
        float height;
        double weight;
        char initial;
    
        // Ask the user to enter their age
        printf("Enter your age: ");
        scanf("%d", &age);  // %d is for an integer
    
        // Ask the user to enter their height
        printf("Enter your height in meters: ");
        scanf("%f", &height);  // %f is for a float
    
        // Ask the user to enter their weight
        printf("Enter your weight in kilogram: ");
        scanf("%lf", &weight);  // %lf is for a double
    
        // Ask the user to enter the first character of their name
        printf("Enter the first character of your name: ");
        scanf(" %c", &initial);  // %c is for a character (the space before %c helps to skip any leftover newline)
    
        // Print the values entered by the user
        printf("\nYou entered:\n");
        printf("Age: %d\n", age);
        printf("Height: %.2f meters\n", height);
        printf("Weight: %.2f kilogram\n", weight);
        printf("Initial: %c\n", initial);
    
        return 0;
    }
    Output
    Enter your age: 15
    Enter your height in meters: 3.423
    Enter your weight in kilogram: 45.21543
    Enter the first character of your name: R
    
    You entered:
    Age: 15
    Height: 3.42 meters
    Weight: 45.22 kilogram
    Initial: R
    What Happens in the Example
    1. Prompting the User:
    printf is used to ask the user to enter their age, height, weight, and initial.
    2. Reading Input:
    scanf("%d", &age); reads an integer type value and stores it in the variable age.
    scanf("%f", &height); reads a float type value and stores it in the variable height.
    scanf("%lf", &weight); reads a double type value and stores it in the variable weight.
    scanf(" %c", &initial); reads a single character and stores it in the variable initial. The space before %c makes sure any leftover newline character is ignored.
    3. Displaying the Input:
    printf shows the values entered by the user, confirming they were read correctly.

    Handling Multiple Inputs in a Single scanf() Function

    We can read multiple values in a single scanf call by specifying multiple format specifiers. Here is an example given below:

    Example
    #include <stdio.h>
    
    int main()
    {
        int num1, num2, num3;
        float num4;
        double num5;
    
        // Ask the user to enter three integer numbers, one float and one double type number
        printf("Enter three integer numbers, one float and one double type number:\n");
        scanf("%d%d%d%f%lf", &num1, &num2, &num3, &num4, &num5);  // %d for integer %f for float and %lf for double
    
        // Print the values entered by the user
        printf("\nYou entered:\n");
        printf("1st number: %d\n", num1);
        printf("2nd Number: %d\n", num2);
        printf("3rd Number: %d\n", num3);
        printf("4th Number: %f\n", num4);
        printf("5th Number: %.8lf\n", num5);
    
        return 0;
    }
    Output
    Enter three integer numbers and one float and one double type number:
    45
    95
    78
    12.33
    8.62143624
    
    You entered:
    1st number: 45
    2nd Number: 95
    3rd Number: 78
    4th Number: 12.330000
    5th Number: 8.62143624

    You can see that we have used %d%d%d%f%lf inside the scanf() function to accept three integer values, one float, and one double value from the keyboard, and store them in the variables num1, num2, num3, num4 and num5 respectively.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.