Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. scanf() / fscanf() / sscanf()

scanf() / fscanf() / sscanf()

Reads input according to a format string and stores the values in variables. scanf() reads from standard input (keyboard), while fscanf() reads from a specified stream (such as a file). Both are defined in <stdio.h>.

Syntax

#include <stdio.h>

// Reads formatted input from standard input (pass the address of each variable).
scanf(format_string, &var1, &var2, ...);

// Reads formatted input from a specified stream.
fscanf(stream, format_string, &var1, &var2, ...);

// Reads formatted input from a string (string parsing).
sscanf(string, format_string, &var1, &var2, ...);

// The return value is the number of items successfully read.
// Returns 0 or EOF on end-of-file input or read failure.
int ret = scanf(format_string, &var);

scanf Format Specifiers and Notes

SpecifierCorresponding TypeDescription
%dint *Reads a decimal integer.
%ffloat *Reads a floating-point number as a float.
%lfdouble *Reads a floating-point number as a double. Unlike printf, the l modifier is required.
%cchar *Reads a single character, including whitespace characters.
%schar *Reads a string delimited by whitespace (spaces, newlines, etc.). Be careful of buffer overflows.
%[width]schar *Reads up to the specified number of characters (e.g., %63s reads up to 63 characters).

Sample Code

#include <stdio.h>

int main(void) {
    // Read an integer (note that you pass the address with &).
    int age;
    printf("Enter your age: ");
    if (scanf("%d", &age) == 1) {
        printf("Age entered: %d\n", age);
    }

    // Read a floating-point number (use %lf for double).
    double price;
    printf("Enter a price: ");
    scanf("%lf", &price);
    printf("Price: %.2f\n", price);

    // Read a string (64 bytes needed: up to 63 characters + null terminator).
    char name[64];
    printf("Enter your name: ");
    scanf("%63s", name); // Specify buffer size minus 1.
    printf("Name: %s\n", name);

    // Read multiple values at once.
    int x, y;
    printf("Enter coordinates (e.g., 3 5): ");
    if (scanf("%d %d", &x, &y) == 2) {
        printf("Coordinates: (%d, %d)\n", x, y);
    }

    // Parse a string with sscanf.
    char record[] = "Taro 20 95.5";
    char person_name[32];
    int  person_age;
    double person_score;
    sscanf(record, "%s %d %lf", person_name, &person_age, &person_score);
    printf("Name: %s, Age: %d, Score: %.1f\n", person_name, person_age, person_score);
    // Outputs: "Name: Taro, Age: 20, Score: 95.5"

    // Read from a file with fscanf.
    FILE *fp = fopen("data.txt", "r");
    if (fp != NULL) {
        int val;
        while (fscanf(fp, "%d", &val) == 1) {
            printf("Read: %d\n", val);
        }
        fclose(fp);
    }

    return 0;
}

Notes

When passing variables to scanf(), always use the address operator &. For strings (char arrays), the array name itself is already an address, so & is not needed. If you use %s without specifying a buffer size, a long input can cause a buffer overflow. Always specify the maximum number of characters (e.g., %63s). For safer line input, consider using fgets() instead.

When scanf() fails to read input, invalid input may remain in the buffer. Always check the return value to verify how many items were read successfully, and handle errors accordingly. If a trailing newline causes issues with a subsequent %c read, add a space before the specifier (e.g., scanf(" %c", &c)) to skip any leading whitespace or newline.

For formatted output, see printf() / fprintf(). For safe string input, see fgets().

If you find any errors or copyright issues, please .