| by Arround The Web | No comments

sscanf() Function in C

In the various programming languages like C, we always encounter programs that contribute to taking the input from the user in the form of any value typed from the keyboard input, which involves the use of the scanf() method. But several other programs exist where we want to read the data formatted from the other string instead of keyboard input. For such types of reading formatted strings from one string to another, we use the sscanf() method. This function overwrites the value in one string with the other string, or when it extracts and breaks the one whole string into two different strings. Suppose we have a string “hi to the world”. We break and store this string into three other strings and store it in another variable; then, we may use the sscanf() method.

Procedure

We might get confused with using the scanf() and sscanf() functions in the C. This article will differentiate between these two functions and show us the syntax and method for implementing the sscanf() method with the help of various examples. Let’s continue with the procedure with the following steps:

Syntax
To implement the sscanf() method, we need to have a complete grip on the syntax of this method. This function takes the following arguments that we need to understand completely:

$ int sscanf (const char *string, const char * format, [ arg1, arg2, ...]);

In the arguments of the sscanf() method, the first argument is the character to pointer type of string that acts as a source for the function to retrieve the data or, in other words, this acts as a buffer. The second argument is the format specifier indicating the data type of the data to be allocated, and the last is the argument list that is as same as the scanf() method. In short, the sscanf() method is used specifically to read the data from the buffer and store this data in the locations as defined in the argument list, where every argument is urged to be a pointer for the variable with the type as specified in the format string (second argument).

Example # 01
We will implement a program in this example to show the working of the sscanf() for reading the strings. To work with the sscanf() method, we would have to import the following two headers files into the programs.

$ # include < stdio.h >
$ # include < string.h >

The two libraries with extension “.h” are represented as headers’ files. This first library is included to use the information regarding reading input and display output in the program. The string “.h” is used since this header file handles the declaration of the functions that deal with strings, memory, and constants. Now, in the main, we will declare and initialize a pointer string with data type char with its assigned value as “David executive 39”. Then we will declare the two other strings with names, “designation” and “name”, respectively, having the memory size equal to ten “10”.

Another variable with data type integer and name age will also be initialized. In the next step, we will call the sscanf() method and read the data from the already declared and initialized pointer type char string. The declared string “David executive 39” has three different items: name, age, and designation. We will read them by following the same procedure as scanf(), where we will read the information by passing the format specifier for that piece of information with its name.

This would be done in the argument list of the sscanf() method as sscanf (string, “%s %s %d”, name, designation, &age), and then we will save it in some other variable with data type as “int”. Following the steps, we have extracted the three different pieces of information from one string. Now, we will display these three items in the output by calling the printf() method.

#include<stdio.h>
#include<string.h>

int main ()
{
    char* string = "David Executive 28";
    char name [10], designation [10];
    int age, extract;

    extract = sscanf (string, "%s %s %d", name, designation, &age);

    printf ("Name: %s\n", name);
    printf("Designation: %s\n", designation);
    printf("Age: %d\n", age);
    return 0;
}

The output depicts that we have extracted from one buffer string the three pieces of information and have written them in the specific argument list locations as name, designation, and age using the sscanf() method.

Example # 02
We will implement another example for the sscanf() method to extract the strings from the other string. First, we will import the required libraries, as follows:

$ # include <stdio.h>
$ #include <stdlib.h>
$ #include <string.h>

These libraries will automatically include the necessary printf, sscanf, and scanf() functions in the program. In the previous example, we had used the char to pointer type string that acted as a buffer or source, so we could retrieve the data from it. But, in this example, we will simply declare a string using strcpy (string_name, “string”). Then we will use this string as the first argument of the sscanf() method to use it as a buffer, so it can be read for extracting the information from the strcpy() and stored in the location as specified by the argument list. Once the information is stored in the respective argument locations using the sscanf() method, we will display that information with names and format specifiers by calling the printf() method.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   int day, year;
   char weekday [20], month [20], string [100];

   strcpy( string, "Tuesday October 25 2022" );
   sscanf( string, "%s %s %d  %d", weekday, month, &day, &year );

   printf ("%s %d, %d = %s\n", month, day, year, weekday);
   
   return (0);
}

We had declared a string using strcpy with the name string as Tuesday, October 25, 2022, and we passed this string as the buffer/source string to the sscanf() function, which extracted the information, such as a month, date, year, and day according to the argument list. This extracted information is then displayed, calling the printf() function.

Conclusion

The sscanf() method is used in the C programming language to replace one string with another. To use this function, one should have clarity on the concept of the sscanf() arguments list. This article clearly explains the step-by-step process, which involves the syntax of the function, required arguments by the function, and where we may use this function to make good logic for our software programs.

Share Button

Source: linuxhint.com

Leave a Reply