Arduino sscanf() Function
In Arduino programming we can store symbols, characters, numbers, or any other data inside a string. To read the data from a string we use the sscanf() function; it not only reads the data but can also store it inside a new variable. Generally, in C programming data is read using Scanf() but using this function we can only read data using console, if one wants to read an input from string or any other text the sscanf() function is used.
Syntax
To read data from a string we must know how the function works. Below is the syntax of sscanf() function.
In the above code, the first argument represents the input which contains the character or string array data which is to be read using the format defined in the second argument. We can list available data in different variables by defining the format in the second argument.
Parameters
- data: This is a pointer to a string which we want to read.
- format: Format specifiers will be used here to read data in a certain way.
- storage_variables: it is used to store the read value from pointers in additional arguments. The variable name must be preceded by the & sign if we need to store values in a regular variable rather than a pointer.
Format Specifiers
The sscanf() function uses different format specifiers that can store data in a certain way. It is a unique set of characters preceded by % sign followed by relevant character symbols. Following symbols are generally used:
Symbol | Type |
s | String |
c | Single char |
d | Decimal int |
e, E, f, g, G | Floating points |
U | Unsigned int |
x, X | Hexadecimal num |
Return Value
- End of File (EOF) error will be returned if the string could not read a proper value.
- -1 is returned if any other type of error is encountered.
- Total number of items read is returned as an integer value by the function.
Examples
Now that we know the syntax and working of the sscanf() function let’s take an example to understand it better.
1. Reading One Item of Same Type
The first example will explain how we can read only a single data type input from a string and store it inside a new variable.
Code
Serial.begin(115200); /*Serial Communication begin*/
char* buffer = "Linuxhint"; /*String is initialized in buffer variable using char*/
char input_string[10]; /*String length defined*/
int Total_input_read; /*variable is initialized to count total input read*/
Total_input_read = sscanf(buffer, "%s" , input_string); /*sscanf function is used to read input string*/
Serial.print("String value stored in buffer: ");
Serial.println(input_string); /*input string is printed on serial monitor*/
Serial.print("No of total items read: ");
Serial.println(Total_input_read); /*no of input read is printed on serial monitor*/
}
void loop()
Here in this code first we begin serial communication to print results on the serial monitor. Next a char array is defined using a variable buffer. Length of array is set to 10, which can vary accordingly. A new variable is initialized which will store the total number of inputs read.
Next using the sscanf() function string will be stored inside a new variable input_string. Here in sscanf() we use format specifier %s because it will only read strings as input. Both results will be printed on the serial monitor.
Output
Output shows a string is taken as input and stored inside a new variable input_string.
2. Reading Multiple Items of Different Types
This example will explain how sscanf() will take data of multiple data types from a single input string.
Code
Serial.begin(115200); /*Serial Communication begin*/
char *data = "28 FEB 2022"; /*New string is initialized*/
int date = 0; /*variable is defined to store date*/
int year = 0; /*variable is defined to store year*/
char month[10]; /*An array is initialized to store month*/
Serial.println(data); /*Input data is printed*/
sscanf(data, "%2d %s %4d", &date, month, &year); /*sscanf will read data from input and store them in separate variables*/
Serial.println(date); /*Read date is printed*/
Serial.println(month); /*Read month is printed*/
Serial.println(year); /*Read year is printed*/
}
void loop() {}
In the above code we have created a data variable that can store the given string. Three different variable dates, year and month will store the read input from the string separately. month variable is initialized with size of 10 which can be modified accordingly.
Next sscanf() function will read the data from input and store them in three different variables. As date and year both represent an integer value so %d symbol is used along with number 2 and 4 which represents how many numbers we want to read. The %s symbol is used with month which will return characters until a white space comes.
If there is no space between data, then we can use %c along with a number that will represent the number of characters we want to read. All the data read will be printed separately using Serial.println().
Output
Output shows the input string. Sscanf() will read data and store them in separate three variables.
Conclusion
Generally, while programming in C language we read input from the user using scanf() function, but it is only limited to the console. To read data from a defined string and store it separately in multiple variables we use sscanf() function in Arduino programming. This write up will assist you to understand sscanf() and read data from defined strings.