scanf() Function

The scanf() function in C stdio.h reads formatted data from the standard input (stdin) and stores the values in the provided locations based on the given format string. It is widely used for capturing user input in console applications.


Syntax of scanf()

</>
Copy
int scanf(const char *format, ...);

Parameters

ParameterDescription
formatA C string containing literal characters, whitespace, and format specifiers that determine how input is interpreted and stored.

Additional arguments are expected to be pointers to pre-allocated storage locations that match the types specified in the format string. The format specifiers dictate the type of data to be read and the expected conversion, such as integers (%d), floating-point numbers (%f), characters (%c), and strings (%s), among others.


Return Value

The function returns the number of input items successfully assigned. This number may be less than expected if a matching failure occurs. If a reading error or end-of-file is encountered before any assignment is made, EOF is returned.


Examples for scanf()

Example 1: Reading an Integer from User Input

This example demonstrates how to use scanf() to read an integer value from standard input.

Program

</>
Copy
#include <stdio.h>

int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}

Explanation:

  1. An integer variable num is declared.
  2. The user is prompted to enter an integer.
  3. scanf() reads the integer from the standard input and stores it in num.
  4. The program prints the entered integer to confirm the input.

Program Output:

Enter an integer: 42
You entered: 42

Example 2: Reading Multiple Values with Mixed Data Types

This example shows how to use scanf() to read an integer, a floating-point number, and a character from user input.

Program

</>
Copy
#include <stdio.h>

int main() {
    int age;
    float height;
    char initial;
    
    printf("Enter age, height and initial (e.g., 30 5.9 A): ");
    scanf("%d %f %c", &age, &height, &initial);
    
    printf("Age: %d, Height: %.1f, Initial: %c\n", age, height, initial);
    return 0;
}

Explanation:

  1. Variables for an integer (age), a float (height), and a character (initial) are declared.
  2. The user is prompted to input values separated by spaces.
  3. scanf() reads the inputs according to the format specifiers %d, %f, and %c and stores them in the respective variables.
  4. The program then prints the values to confirm successful input.

Program Output:

Enter age, height and initial (e.g., 30 5.9 A): 30 5.9 A
Age: 30, Height: 5.9, Initial: A

Example 3: Reading a String Without Whitespace

This example demonstrates how to use a scanset with scanf() to read a single word (a string without whitespace).

Program

</>
Copy
#include <stdio.h>

int main() {
    char word[20];
    
    printf("Enter a single word: ");
    scanf("%19s", word);
    
    printf("You entered: %s\n", word);
    return 0;
}

Explanation:

  1. A character array word of size 20 is declared.
  2. The user is prompted to enter a single word.
  3. scanf() reads up to 19 characters using the format specifier %19s, ensuring space for the null terminator.
  4. The entered word is printed to confirm the input.

Program Output:

Enter a single word: hello
You entered: hello

Example 4: Handling Input Failure with scanf()

This example illustrates how to verify the return value of scanf() to detect input errors or mismatches.

Program

</>
Copy
#include <stdio.h>

int main() {
    int num;
    
    printf("Enter an integer: ");
    if (scanf("%d", &num) != 1) {
        printf("Input error! Please enter a valid integer.\n");
    } else {
        printf("You entered: %d\n", num);
    }
    
    return 0;
}

Explanation:

  1. An integer variable num is declared.
  2. The user is prompted to input an integer.
  3. The return value of scanf() is checked to ensure that one input item was successfully read.
  4. If the input does not match the expected format, an error message is printed; otherwise, the entered value is displayed.

Program Output:

Enter an integer: abc
Input error! Please enter a valid integer.