Open In App
Related Articles

scanf in C

Improve Article
Save Article
Like Article

In C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments.

  • It accepts character, string, and numeric data from the user using standard input.
  • scanf also uses format specifiers like printf.

scanf Syntax

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

int scanf( const char *format, ... );


  • int is the return type.
  • format is a string that contains the format specifiers(s).
  • “…” indicates that the function accepts a variable number of arguments.

Example 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.

To know more about format specifiers, refer to this article – Format Specifiers in C


int var;
scanf(“%d”, &var);

The scanf will write the value input by the user into the integer variable var.

Return Value of scanf

The scanf in C returns three types of values:

  • >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.

Why &?

While scanning the input, scanf needs to store that input data somewhere. To store this input data, scanf needs to known the memory location of a variable. And here comes the ampersand to rescue.

  • & is also called as address of the operator.
  • For example, &var is the address of var.

Example of scanf

Below is the C program to implement scanf:


// C program to implement
// scanf
#include <stdio.h>
// Driver code
int main()
    int a, b;
      printf("Enter first number: ");
      scanf("%d", &a);
      printf("Enter second number: ");
      scanf("%d", &b);
      printf("A : %d \t B : %d" ,
            a , b);
    return 0;


Enter first number: 5
Enter second number: 6
A : 5      B : 6

Related Article:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 May, 2023
Like Article
Save Article
Similar Reads