C scanf() Function | Read Formatted Input from Standard Input

The scanf() function in C is a standard input function that reads formatted input from the standard input (stdin) stream.

It is part of the C standard library (stdio.h) and is commonly used to read various types of data from the user.

Table of Contents

  1. Introduction
  2. scanf() Function Syntax
  3. Understanding scanf()
  4. Examples
    • Reading an Integer
    • Reading a Floating-point Number
    • Reading a String
  5. Real-World Use Case
  6. Conclusion

Introduction

The scanf() function is a powerful input function that allows you to read formatted data from the standard input stream. It is commonly used to take user input in C programs.

scanf() Function Syntax

The syntax for the scanf() function is as follows:

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

Parameters:

  • format: A C string that contains one or more format specifiers, which define the type and format of the input to be read.
  • ...: The additional arguments correspond to the format specifiers in the format string and represent the addresses of variables where the input will be stored.

Returns:

  • The function returns the number of items successfully read and assigned. This count does not include items that were read but not assigned.

Throws:

  • This function does not throw exceptions, but it may return a value of EOF (end of file) if an input failure occurs before any input can be read.

Understanding scanf()

The scanf() function reads data from the standard input stream and stores the data into the variables provided as arguments. The function uses format specifiers to determine the type and format of the input to be read.

Examples

Reading an Integer

To demonstrate how to use scanf() to read an integer from the user, we will write a simple program.

Example

#include <stdio.h>

int main() {
    int number;

    // Prompt the user for input
    printf("Enter an integer: ");

    // Read the integer from the user
    scanf("%d", &number);

    // Print the entered integer
    printf("You entered: %d\n", number);

    return 0;
}

Output:

Enter an integer: 42
You entered: 42

Reading a Floating-point Number

This example shows how to use scanf() to read a floating-point number from the user.

Example

#include <stdio.h>

int main() {
    float number;

    // Prompt the user for input
    printf("Enter a floating-point number: ");

    // Read the floating-point number from the user
    scanf("%f", &number);

    // Print the entered floating-point number
    printf("You entered: %f\n", number);

    return 0;
}

Output:

Enter a floating-point number: 3.14
You entered: 3.140000

Reading a String

This example demonstrates how to use scanf() to read a string from the user.

Example

#include <stdio.h>

int main() {
    char name[50];

    // Prompt the user for input
    printf("Enter your name: ");

    // Read the string from the user
    scanf("%s", name);

    // Print the entered string
    printf("Hello, %s!\n", name);

    return 0;
}

Output:

Enter your name: Ramesh
Hello, Ramesh!

Real-World Use Case

Reading Multiple Values

In real-world applications, the scanf() function can be used to read multiple values of different types from the user.

Example

#include <stdio.h>

int main() {
    char name[50];
    int age;
    float salary;

    // Prompt the user for input
    printf("Enter your name, age, and salary: ");

    // Read the string, integer, and float from the user
    scanf("%s %d %f", name, &age, &salary);

    // Print the entered values
    printf("Name: %s, Age: %d, Salary: %.2f\n", name, age, salary);

    return 0;
}

Output:

Enter your name, age, and salary: Ramesh 25 50000.50
Name: Ramesh, Age: 25, Salary: 50000.50

Conclusion

The scanf() function is an input function that allows you to read formatted data from the standard input stream.

By understanding and using this function, you can efficiently handle user input in your C programs.

It is important to be familiar with the various format specifiers and how to use them to read different types of data correctly.

Comments