Problem with sscanf() parsing webhook response (C help)

I’m seeing a difference in behavior between sscanf on my photon and on my command line. I’ve got a web hook that returns “74.01~-6” in the data, and am trying to parse it like this:

void outsideTempHandler(const char *event, const char *data) {
    // data = "74.09~-6"  // temp and TZ offset
    int tz, n;
    float ot;
    n = sscanf(data, "%f~%d", &ot, &tz);
    Serial.println(String::format("Temp = %f, TZ = %d, sscanf items = %d", ot, tz, n));
}

The scan fails and I always get zeros in my debug print there.
At first I thought maybe I needed a fancier sscanf formatting string, but the following works perfectly fine with “gcc” locally:

#include <stdio.h>
int main() {
  char str[] = "76.04~-6";
  float temp;
  int tz,n;
  n = sscanf(str, "%f~%d", &temp, &tz);
  printf("Scanned %d things, temp %f, tz %d\n", n, temp, tz);
}

Can anyone see what I’m doing wrong?

The problem is that the sscanf in standard C library does not support %f. It’s weird and annoying, but true.

2 Likes

Wow, thank you. Seems to work with my gcc on Ubuntu, guess I expect differences.

@rickkas7, could you inquire at Particle what the cost (RAM, time, …) would be to bring that to the platform?
It has been discussed several times, but without official statement what to do about it :wink:

https://community.particle.io/search?q=sscanf%20float