How to Read Data Using sscanf() in C?
Last Updated :
28 May, 2024
In C, sscanf() function stands for "String Scan Formatted". This function is used for parsing the formatted strings, unlike scanf() that reads from the input stream, the sscanf() function extracts data from a string. In this article, we will learn how to read data using sscanf() function in C.
Example:
Input:
char *str = "Ram Manager 30";
Output:
Name: Ram
Designation: Manager //parsed string
Age: 30
Read Data Using sscanf Function in C
In C, the sscanf() function can be used to read formatted data from a string rather than a standard input or keyboard. Pass the string to be parsed, a format specifier that defines the expected data types and structure of the input string (like %d, %f, %c, etc), and the list of variables where we want to store the formatted data as a parameter to the sscanf() functions to get the parsed string.
Syntax of sscanf() in C
sscanf(input_str, format, store1, store2, ...);
Here,
- input_str is the string from which data needs to be read.
- format tells the type and format of data to be read
- store1, store2, ... represents the variables where the read data will be stored.
C Program to Read Data Using sscanf()
The below program demonstrates how we can read data using sscanf() function in C.
C
// C program to Read Data Using sscanf()
#include <stdio.h>
#include <string.h>
int main()
{
// Define a string containing the data to be parsed.
char* str = "Ram Manager 30";
// Define variables to hold the parsed data.
char name[10], designation[10];
int age, ret;
// Use sscanf to parse the string into the variables.
ret = sscanf(str, "%s %s %d", name, designation, &age);
// Print the parsed data.
printf("Name: %s\n", name);
printf("Designation: %s\n", designation);
printf("Age: %d\n", age);
return 0;
}
OutputName: Ram
Designation: Manager
Age: 30
Time complexity: O(n), where n is the length of the input string.
Auxilliary Space: O(1)
Explanation: In this program, sscanf() reads three items from the string str, so 3 will be assigned to ret. The printf() function is then used to display name, designation, and age.
Note: If the input string does not match the specified format, sscanf() might fail to extract data correctly and may lead to parsing errors.
Similar Reads
How to Read Input Until EOF in C? In C, reading input until the End of the File (EOF) involves reading input until it reaches the end i.e. end of file. In this article, we will learn various methods through which we can read inputs until EOF in C.Reading Input Until EOFRead Input Until EOF Using getchar()Read Input Until EOF Using f
5 min read
Why to use fgets() over scanf() in C? Any time you use an *f function, whether it be printf, scanf, or their derivatives (fprintf, fscanf, etc...), you are doing more things than you might realize. Not only are you reading (or writing) something, but-and here's the problem- you are interpreting it. The format string can be thought of as
5 min read
How to write in a file using fputs() in C fputs() is a function declared in stdio.h header file. It is used to write the contents of the file. The function takes 2 arguments. The first argument is a pointer to the string which is to be written and the second argument is the pointer of the file where the string is to be written. It returns 1
2 min read
How to Read From a File in C? File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. In this article,
2 min read
C Program to Compare Two Strings Using Pointers In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings whil
2 min read
How to Read a Struct from a Binary File in C? The structure in C allows the users to create user-defined data types that can be used to group data items of different types into a single unit. We can save this structure in a file using C file handling. In this article, we will learn how we can read a struct from a binary file in C. Read a Struct
2 min read