Issue with sscanf not parsing correctly

Alexandre Raymond alexandre.j.raymond@gmail.com
Mon Aug 16 02:38:11 GMT 2021


Greetings!

I have been developing on a Teensy 4.1 platform, which I'm told uses
newlib under the hood, and encountered a strange issue with sscanf,
where different parameter orderings yield different results with
stdint types.

Here is a minimal code sample which exhibits the issue:
---8<---

#include <stdio.h>
#include <stdint.h>

void setup() {
    uint8_t i2c_addr = 1;
    uint16_t mem_addr = 1;
    uint8_t len = 1;
    int ret;

    Serial.println(String(i2c_addr) + "," + String(mem_addr) + "," +
String(len));
    //prints: 1,1,1

    const char *cmd = "1 2 3\n";

    // This is the order I intended to scan the string in
    ret = sscanf(cmd, "%hhu %hu %hhu", &i2c_addr, &mem_addr, &len);
    Serial.println(String(ret) + "- " + String(i2c_addr) + "," +
String(mem_addr) + "," + String(len));
    // prints: 3- 1,0,3

    // I just swap the order of the target variables around
    ret = sscanf(cmd, "%hhu %hhu %hu", &i2c_addr, &len, &mem_addr);
    Serial.println(String(ret) + "- " + String(i2c_addr) + "," +
String(len) + "," + String(mem_addr));
    // prints: 3- 1,2,3
}

void loop() { }

--8<---

Would you have any idea what may be causing this?

Thanks in advance for your help!
Alexandre


More information about the Newlib mailing list