Arduino sscanf Function

Arduino sscanf Function

This tutorial will discuss reading formatted data from a character array or string using the sscanf() function in Arduino.

the Arduino sscanf() Function

We can save numbers, symbols, characters, and other data inside a text or string file. We can use the sscanf() function to read the data in a specific format from a text, string, or character array.

The basic syntax of the sscanf() function is below.

sscanf(char *data, char *format, ...);

In the above code, data contains the given string or character array we want to read using the format defined in the second argument. After defining the format, we can list the number of variables we want to store the formatted data.

For example, consider we have a string that contains the date of an event, and we want to separate the date, month, and year and store them in separate variables. See the code below.

char *data = "28 April 2022";
int date = 0;
int year = 0;
char month[10];

void setup() {
  Serial.begin(9600);
  Serial.println(data);
  sscanf(data, "%2d %s %4d", &date, month, &year);
  Serial.println(date);
  Serial.println(month);
  Serial.println(year);
}

void loop() {}

Output:

28 April 2022
28
April
2022

We created the data variable to store the given string in the above code. The date, year, and month variables store day, year, and month values separately.

We initialized the month variable with a size of 10, but we can change its size according to the given string that we want to save in the variable. We used the Serial.begin() function to initialize the serial monitor of Arduino, which we will use to display the result.

To get an integer value, we use the %d symbol, and the number 2 with it defines how many numbers we want to read. We use the %s symbol to get a string, which will return the characters until white space.

If there is no space in the given string, we can use the %c and a number to specify how many characters we want to read. We cannot use %s because there is no white space, which will not return the expected result.

After the format, to save the result in an integer, we have to use the & symbol, and for a string or character array, we only have to use the variable name. In the above example, we used the white space as the delimiter to format the data, but we can also use any character, symbol, or number.

For example, consider if we have a comma in place of the white space inside the date string.

To extract the required result, we have to use the comma inside the sscanf() function in place of the white space. We also have to use the %c symbol instead of the %s symbol because there is no white space present in the string.

For example, let’s repeat the above example with a different delimiter. See the code below.

char *data = "28,April,2022";
int date = 0;
int year = 0;
char month[10];

void setup() {
  Serial.begin(9600);
  Serial.println(data);
  sscanf(data, "%2d,%5c,%4d", &date, month, &year);
  Serial.println(date);
  Serial.println(month);
  Serial.println(year);
}

void loop() {}

Output:

28,April,2022
28
April
2022

We use the %5c symbol inside the sscanf() function in the above code because we only want to read five characters. If we read more than five characters, the next output will change.

As we can see, the output is the same as the output of the first example. We can also use other symbols for formatting the data like %f for floating-point numbers, %o for octal numbers, %x for hexadecimal numbers, and %u for an unsigned integer.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - Arduino Function

  • Arduino Return Array From Function
  • Generate Random Numbers in Arduino
  • Arduino millis() Function