✏️ Explanatory Question

What is scanf()?

👁 1,005 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

C Programming Language

What is scanf() in C?

The scanf() function is one of the most commonly used input functions in C programming. It allows a program to receive data from the user through the keyboard during program execution.

Answer: scanf() is a standard library function declared in the header file. It is used to read formatted input from the standard input device (usually the keyboard) and store it in variables.

Syntax


scanf("format_string", &variable1, &variable2, ...);
Part Description
scanf Name of the input function.
format_string Specifies the type of data to be read using format specifiers such as %d, %f, %c, and %s.
&variable Represents the memory address of the variable where the input value will be stored. (The & operator is generally required for all variables except character arrays/strings.)

Header File


#include 

Before using scanf(), you must include the header file because it contains the function declaration (prototype) of scanf().

Example 1: Read an Integer


#include 

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Your age is %d", age);

    return 0;
}

Sample Output


Enter your age: 20
Your age is 20

Example 2: Read Multiple Values


#include 

int main()
{
    int roll;
    float marks;

    printf("Enter roll number and marks: ");
    scanf("%d %f", &roll, &marks);

    printf("Roll Number = %d\n", roll);
    printf("Marks = %.2f", marks);

    return 0;
}

Sample Output


Enter roll number and marks: 101 89.75
Roll Number = 101
Marks = 89.75

Example 3: Read a Character


#include 

int main()
{
    char grade;

    printf("Enter your grade: ");
    scanf("%c", &grade);

    printf("Grade = %c", grade);

    return 0;
}

Sample Output


Enter your grade: A
Grade = A

Example 4: Read a String


#include 

int main()
{
    char name[30];

    printf("Enter your name: ");
    scanf("%s", name);

    printf("Hello %s", name);

    return 0;
}

Sample Output


Enter your name: Rumman
Hello Rumman

Common Format Specifiers Used with scanf()

Format Specifier Data Type Example Input
%d Integer 25
%f Float 45.67
%lf Double 123.456
%c Character A
%s String (Word) Hello

Return Value of scanf()

The scanf() function returns the number of input items that were successfully read and assigned to variables.


#include 

int main()
{
    int num;
    int result;

    printf("Enter a number: ");
    result = scanf("%d", &num);

    printf("scanf() returned %d", result);

    return 0;
}

Sample Output


Enter a number: 50
scanf() returned 1
RETURN VALUE
scanf()Number of Successfully Assigned Input Values

Advantages of scanf()

Advantages

  • Accepts user input during program execution.
  • Supports multiple data types.
  • Can read multiple values in a single statement.
  • Simple and easy to use.
  • Useful for interactive console applications.
  • Supports formatted input using format specifiers.

Common Mistakes

Avoid These Mistakes

  • Forgetting to include .
  • Omitting the & operator for numeric and character variables.
  • Using incorrect format specifiers.
  • Using %s to read strings containing spaces.
  • Ignoring the return value of scanf() when validating input.

Best Practices

  • Always verify the return value of scanf().
  • Use the correct format specifier for each data type.
  • Remember to use the address operator (&) for variables, except character arrays.
  • Use fgets() instead of scanf("%s") when reading strings containing spaces.

Difference Between printf() and scanf()

Feature printf() scanf()
Purpose Displays output. Reads input.
Direction Program → Screen Keyboard → Program
Return Value Number of characters printed. Number of successfully assigned input values.
Uses & Operator No Yes (except for character arrays).

Prerequisites

Before Learning This Topic

  • Basic understanding of C program structure.
  • Knowledge of variables and data types.
  • Understanding of memory addresses and the address-of (&) operator.
  • Basic familiarity with format specifiers.
  • Knowledge of the header file.

Interview Questions

  1. What is the purpose of the scanf() function?
  2. Which header file contains the declaration of scanf()?
  3. Why is the & operator used with scanf()?
  4. What value does scanf() return?
  5. Why can't scanf("%s") read a string containing spaces?
  6. What is the difference between scanf() and fgets()?

Key Takeaway

scanf() is a standard input function in C used to read formatted data from the keyboard. It is declared in the header file, supports multiple data types through format specifiers, and stores user input in variables. It returns the number of successfully assigned input values, making it useful for validating user input.