Arduino: sscanf compilation error

Created on 30 Jun 2015  Â·  29Comments  Â·  Source: esp8266/Arduino

I recently tried to use sscanf, however the compiler threw an undefined reference error.
Replacing sscanf with os_sprintf as recommended in issue #404 did not solve it, instead it created a watchdog timer reboot.
Here is the code I was using:

void setup() {
  Serial.begin(115200);
  Serial.println("starting");
  char *data = "1234";
  int i = 0;
  sscanf(data,"%d", &i);
  Serial.println(i);
}
void loop() { }

Any suggestions would be appreciated.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

libc enhancement

Most helpful comment

Implemented in #1752

All 29 comments

You need os_scanf and not os_sprintf

On Mon, Jun 29, 2015, 7:04 PM nicjohnston [email protected] wrote:

I recently tried to use sscanf, however the compiler threw an undefined
reference error.
Replacing sscanf with os_sprintf as recommended in issue #404
https://github.com/esp8266/Arduino/issues/404 did not solve it, instead
it created a watchdog timer reboot.
Here is the code I was using:

void setup() {
Serial.begin(115200);
Serial.println("starting");
char *data = "1234";
int i = 0;
sscanf(data,"%d", &i);
Serial.println(i);
}void loop() { }

Any suggestions would be appreciated.

—
Reply to this email directly or view it on GitHub
https://github.com/esp8266/Arduino/issues/488.

Thanks for the reply, however I just tried os_scanf, and it threw the following error

error: 'os_scanf' was not declared in this scope

what are you trying to do ?
scanf is intended to read an input from the user. If you want to take the content of "i" and put it in the char buffer "data", then you would do :

void setup() {
Serial.begin(115200);
Serial.println("starting");
char *data = "";
int i = 0;
sprintf(data,"%d", i);
Serial.println(i);
}

Serial output should be :

Starting
0

Also, don't forget that sprintf on arduino doesnt support float or double. Just in case you wanted to use these types.

I am attempting to extract an integer value from a char buffer into a variable; I probably should have specified this sooner.
This issue surfaced while attempting to compile the ArduinoIMU example sketch from this library.
Does this esp8266 library have support for sscanf? If not, could you point me in the right direction to add it myself?
Thanks.

Ok, so this might be a dirty hack, but can you try :

char *buff2 = "24";
int i = ((String)(buff2)).toInt();

Worked fine for me even tho it made a warning.

String is a class not a type.
warning free:

char buff2[] = "24";
int i = String(buff2).toInt();
Serial.println(i);

Thanks for reminding me about toInt, however the toInt function doesn't support parsing multiple variables from a char array.
This should have been in my previous post, but a more representative string is "1500,2100,1200,0". The example code used a single variable string to make debugging easier.
I could loop through the array and find every delimiting character in order to split it up into separate arrays and then convert those to integers using the toInt function, however I use sscanf to parse data in many of the sketches I would like to port to the ESP8266. Because of this, either a drop in replacement for sscanf or a patch for sscanf would be preferable.

completely agree with @nicjohnston, we should implement siscanf(...)

Any news about sscanf?

I'm looking to parse an HTTP Date header - "Thu, 15 Oct 2015 08:57:03 GMT" - into day, hours, minutes etc - I'd like sscanf too.

any ideas for a workaround yet?

@lighthousebulb, I guess current ugly workaround is to use Arduino String.

@a-andreyev How do you suppose to use String instead of sscanf?

+1 for sscanf as its available on "standard" Arduino.

Same issue. I'd need sscanf, too.

+1.Same issue. I'd need sscanf, too.

while this triggers wdt resets:

void setup() {
  Serial.begin(115200);
  Serial.println("starting");
  char *data = "1234";
  int i = 0;
  sprintf(data,"%d", &i);
  Serial.println(i);
}
void loop() { }

this doesn't:

void setup()
{
  Serial.begin(115200);
  Serial.println("starting");
  char data[20];
  int i = 0;
  sprintf(data, "%d", &i);
  Serial.println(i);
}

void loop()
{

}

@Duality4Y "1234" is const char and can not be modified.
note: your issues has noting to do with sscanf

i am just saying that sprintf works, but the way nicjohnston was using it (probably) was with a const char * and that doesn't work.

I assumed he just replaced his sscanf with sprintf.
(he mentions this in the first post)

sscanf and sprintf have totally different functions using as a replaced is not possible.
the SDK not provide a sscanf Implementation, and its not a small functionality.
if some one find code that do sscanf under LGPL licence (or compatible) we can adapted it to the ESP.

I see my mistake :) sorry for the confusion!

if I understand it correctly he wants to convert a series of comma seperated values to series of ints.
you could probably do that with atoi and strtok_r.
little bit advanced though :)

but yea sscanf would be nice to have!

ok here is a thing i copy pasted together (me being way lazy), this works on your case.
this is not tested at all!
it came from here: http://mirror.fsf.org/pmon2000/2.x/src/lib/libc/
and it is probably tested there.
I can't garantee that it doesn't have bugs. :)

http://pastebin.com/N4d029VQ

Implemented in #1752

Woohoo, thank you!

Merged and available in git version.

I would really like to use this, how can I try it out? I checked out the latest feature/libc head to PlatformIO's directory but I just got "undefined reference to sscanf"...

@igrr Has sscanf() been implemented in the latest build? I still get undefined reference to sscanf error.

@Duality4Y Thanks Robert! This helped in the meantime. It doesn't work as is (compilation errors due to some strange blank characters in that file). I've cleaned it up at put it here for anyone who needs it: sscanf.h & sscanf.cpp. Cheers :+1:

Was this page helpful?
0 / 5 - 0 ratings