C sscanf() Function | Parse Strings into Variables

Introduction

The sscanf() function in C is a standard library function that reads formatted input from a string. It is part of the C standard library (stdio.h) and is used to parse strings according to a specified format.

sscanf() Function Syntax

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

int sscanf(const char *str, const char *format, ...);

Parameters:

  • str: A pointer to the input string that contains the data to be parsed.
  • format: A C string that contains format specifiers. These specify the expected format of the input data in the string.
  • ...: Additional arguments that correspond to the format specifiers in the format string. These are pointers to variables where the parsed values will be stored.

Returns:

  • The function returns the number of input items successfully matched and assigned. If an input failure occurs before any matching, EOF is returned.

Understanding sscanf()

The sscanf() function reads data from the provided string (str) and stores the parsed values into the variables provided as arguments. It uses format specifiers to determine the type and format of the input data. Each format specifier begins with a percent sign (%) and is followed by a character that specifies the type of data (e.g., d for integers, f for floating-point numbers, s for strings).

Examples

Reading an Integer from a String

To demonstrate how to use sscanf() to read an integer from a string, we will write a simple program.

Example

#include <stdio.h>

int main() {
    char input[] = "42";
    int number;

    // Read the integer from the string
    sscanf(input, "%d", &number);

    // Print the parsed integer
    printf("The number is: %d\n", number);

    return 0;
}

Output:

The number is: 42

Reading a Floating-point Number from a String

This example shows how to use sscanf() to read a floating-point number from a string.

Example

#include <stdio.h>

int main() {
    char input[] = "3.14";
    float number;

    // Read the floating-point number from the string
    sscanf(input, "%f", &number);

    // Print the parsed floating-point number
    printf("The number is: %f\n", number);

    return 0;
}

Output:

The number is: 3.140000

Reading a String from Another String

This example demonstrates how to use sscanf() to read a string from another string.

Example

#include <stdio.h>

int main() {
    char input[] = "Ramesh Fadatare";
    char name[50];

    // Read the string from the input string
    sscanf(input, "%s", name);

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

    return 0;
}

Output:

Hello, Ramesh!

Real-World Use Case

Parsing Multiple Values

In real-world applications, the sscanf() function can be used to parse multiple values of different types from a single string.

Example

#include <stdio.h>

int main() {
    char input[] = "Ramesh Fadatare 25 50000.50";
    char name[50];
    int age;
    float salary;

    // Parse the string, integer, and float from the input string
    sscanf(input, "%s %d %f", name, &age, &salary);

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

    return 0;
}

Output:

Name: Ramesh, Age: 25, Salary: 50000.50

Conclusion

The sscanf() function allows you to read formatted data from a string, similar to how scanf() reads formatted input from standard input. This function is particularly useful for parsing strings that contain structured data.

Comments