Open In App

scanf in C

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.

Example:

C
#include <stdio.h>

int main() {
    int n;

    // Reading an integer input
    scanf("%d", &n); 
    printf("%d", n);
    return 0;
}


Output

10 (Enter by user)
10

In the above program, scanf("%d", &n) reads an integer from the keyboard and stores it in the integer variable n. The %d format specifier indicates that an integer is expected, and &n provides the memory address of n so that scanf can store the input value there.

Syntax

The syntax of scanf() in C is similar to the syntax of printf().

C
scanf("format", address_of_args... );

Parameters:

  • format: It is the format string that contains the format specifiers(s).
  • address_of_args: Address of the variables where we want to store the input.

Return Value:

  • >0: The number of values converted and assigned successfully.
  •   0: No value was assigned.
  • <0: Read error encountered or end-of-file (EOF) reached before any assignment was made.

We use & operator to find the address of the variables by appending it before the variable name and format specifier to recognize which type of data to be store.

Examples format specifiers recognized by scanf:

%d to accept input of integers.
%ld to accept input of long integers
%lld to accept input of long long integers
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.

If you're interested in learning more about input handling and integrating it into complex data structures, the C Programming Course Online with Data Structures covers practical applications of input functions in C.

Examples of scanf

The below examples demonstrate the use of scanf for different types of input:

Reading Floating Point Value

C
#include <stdio.h>

int main() {
    float f;
  
    // Reading floating number 
    // and store in the float
    // variable f
    scanf("%f", &f);  
    printf("You entered: %f", f);
    return 0;
}


Output

3.21 (Enter by user)
3.210000

In the above program, scanf("%f", &f) statement reads a floating-point number from user input and stores it in the variable f. The %f format specifier ensures the correct interpretation of the input, and &f passes the memory address of variable f to store the value.

Take Two Integers as Input

C
#include <stdio.h>

int main() {
    int a, b;
  
    // Reading two integers and storing
    // them in a and b
    scanf("%d %d", &a, &b);  
    printf("%d %d", a, b);
    return 0;
}


Output

3 7 (Enter by user)
3 7

Reading Text (Strings)

C
#include <stdio.h>

int main() {
    char name[50];
  
    // Reading a string
    scanf("%s", name);  
    printf("%s", name);
    return 0;
}


Console

Geeks For Geeks (Enter by user)
Geeks

In the above example, we read a single input until the first space, so when "Geeks For Geeks" is entered, only "Geeks" will be stored in name. Also, we don't need to use the &operator for the address of name.

Scanset Characters

In C, scanf() provides a feature called scanset characters, which allows you to read custom pattern of input. For example, reading only specific characters, reading text with whitespaces, etc.

Syntax:

C
scanf("%[set]", variable);
  • set: A sequence of characters that defines the pattern. This can include individual characters, ranges of characters (e.g., a-z for lowercase letters), or negated sets (e.g., [^0-9] to exclude digits).

Example: Reading Text with Whitespace

C
#include <stdio.h>
int main() {
    char name[50];
  
    // Reading a string with 
    // whitespaces
    scanf("%[^\n]s", name);  
    printf("%s", name);
    return 0;
}


Output

Hello Geek (Enter by user)
Hello Geek

In the above example, statement scanf("%[^\n]s", name) reads an entire line, including spaces, until a newline (\n) is encountered. This allows storing full names like "Hello Geek" in the name array. The program then prints the entered name using printf().


Next Article
Article Tags :

Similar Reads