Issue with sscanf not parsing correctly

Alexandre Raymond <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <CAD4yBunQ0L0+=mYr07QADRR8e2bnTLvZAHFc0v2=qUsmTetxfg@mail.gmail.com>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.