C++ is a fast and one of the oldest programming languages ideal for the development of operating systems. It offers high performance and great speed, and covers various types of applications. It provides a very advanced syntax, making itself complex and hard to learn. Its built-in functionality, however, is quite helpful to create any kind of application. Converting numbers from strings or strings from numbers is a quite confusing task. Therefore, we need some functions that can be used with the conversion of num to str or str to num. Here, we will learn to use the sscanf function to convert an input to a specified type of output.
The Sscanf C++ Function
The sscanf function is one of the most commonly used C++ functions for converting numbers into strings. It takes an input from the user or the buffer, converts the string into an int, and stores it in a specified variable. In simple words, it interprets the input according to the format and stores the resultant value in a specific location. If performs a similar function as scanf, but reads the input from the buffer instead of the standard input.
See the following complete syntax of the sscanf function provided by the C++ programing language:
The sscanf function takes a minimum of two input arguments and a list of optional arguments. The optional argument means that they can be skipped if not necessary. It reads the data from the “input” buffer in the “format” format into the location specified by the “argument_list”. The “input” parameter or argument represents the string of the function which is used to retrieve the data. The “format” parameter represents the format string that specifies how to read the input from a buffer.
The format of the “format” specifier is as follows:
It starts with a % sign followed by * which is an assignment suppression character. The width and length fields are optional and they can be skipped. If provided, the sscanf function only consumes that maximum number of characters specified by the “width”. The “length” field defines the receiving argument size. The specifier_name holds the specifier character (c, s, d, etc.) in specifying the conversion type. The “argument_list” can hold at least one additional argument. It depends on the “format” string.
The sscanf function returns the three types of results – the successfully assigned receiving arguments in case of a successful function, zero in case of matching failure, and EOF in case of input failure. Now, let us explore some useful examples to learn the usage of the sscanf function of C++.
Example 1:
This is a very simple and basic example of the sscanf function. A string is assigned to a variable which is passed to the sscanf function. The “int a” is defined to hold the result of the sscanf function. See the following given code and understand each line of code.
After including the iostream header file, the main() method is initiated. The “s = 104785” string is initialized and passed to sscanf function sscanf(s, “%d”, &a). The %d represents the conversion format. And “a” is provided to hold the result.
using namespace std;
int main()
{
const char* s = "104785";
int a;
sscanf(s, "%d", &a);
cout << "\nThe converted value of "<<s<<" is : " << a << endl;
return 0;
}
The output can be seen here. The “104785” string is converted into the “104785” decimal number.
Example 2:
In this example, we read the float and double float using the same sscanf function. A string is assigned to a variable which is passed to the sscanf function for format conversion. See the following code to understand the function of sscanf with float and double float numbers.
Note that in the previous example, %d is used as a “format specifier”. In this example, %f and %lf are used. The %f is used to convert the input to a float number and %lf is used to convert the number into a double float number.
#include <iostream>
using namespace std;
int main()
{
const char* s = "104785.54";
float a;
sscanf(s, "%f", &a);
printf("\nThe FLOAT value is : %f", a);
double b;
sscanf(s, "%lf", &b);
printf("\nThe DOUBLE FLOAT value is : %f", b);
return 0;
}
Check the output given in the following:
Note that the output after the decimal place is different for float and for the double float. This is because the float provides the closest approximate result and it is convenient, fast, and precise. While the double float provides the exact calculated result as it is all about reliability and accuracy. However, it is a bit slower than float.
Example 3:
Let us play with some array data. In this example, the input is provided in the form of an array and is converted into simple strings. The %s format specifier is used to convert the input into a string. So, we use the %s as a format specifier here. Two empty arrays are defined to hold the output – fn[10] and ln[10]. The following is the code.
Here, a name consisting of first name and last name is assigned to an array buf[] and is passed to the sscanf function, converting each item of the array into a string. When you execute the code program, you’ll notice each item of the array separately. As there are only two output fields defined (fn and ln), the two array items are shown by the program. The fn[10] array holds the first name “Daniyal”. And the ln[10] array holds the last name “Privater”.
int main ()
{
char fn[10], ln[10];
char buf[] = "Daniyal Privater";
sscanf(buf, "%s %s", fn, ln);
printf("The first name is : %s \nThe Last name is : %s\n", fn, ln);
return 0;
}
The output of this example is shown in the following:
As you can see, the array value “Daniyal Privater” is converted to two strings – “Daniyal” and “Privater” – by the %s format specifier.
Conclusion
This tutorial is about the sscanf function. Here, we learned how to use the sscanf function efficiently to convert the data. The sscanf function takes the input from the buffer and converts it into the specified output type defined by the “format” specifier. It takes three or more three arguments depending on the “format” specifier. The input value is the first parameter, and the format specifier is the second. It instructs the function of the type of conversion. The third parameter is optional. It can be one argument or a list of multiple arguments depending on the “format” specifier. It returns the three types of results – the receiving arguments, zero, or EOF(end of file). The sscanf is a very simple and easy function of C++ that is used for conversions.