Skip to content

sscanf compilation error #488

@nicjohnston

Description

@nicjohnston

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.

Activity

chadouming

chadouming commented on Jun 30, 2015

@chadouming
Contributor

You need os_scanf and not os_sprintf

On Mon, Jun 29, 2015, 7:04 PM nicjohnston notifications@github.com 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
#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
#488.

nicjohnston

nicjohnston commented on Jun 30, 2015

@nicjohnston
Author

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
chadouming

chadouming commented on Jun 30, 2015

@chadouming
Contributor

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.

nicjohnston

nicjohnston commented on Jun 30, 2015

@nicjohnston
Author

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.

chadouming

chadouming commented on Jun 30, 2015

@chadouming
Contributor

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.

Links2004

Links2004 commented on Jun 30, 2015

@Links2004
Collaborator

String is a class not a type.
warning free:

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

nicjohnston commented on Jun 30, 2015

@nicjohnston
Author

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.

a-andreyev

a-andreyev commented on Aug 12, 2015

@a-andreyev

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

Toshik

Toshik commented on Oct 23, 2015

@Toshik
Contributor

Any news about sscanf?

torntrousers

torntrousers commented on Oct 25, 2015

@torntrousers
Contributor

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.

lighthousebulb

lighthousebulb commented on Dec 11, 2015

@lighthousebulb

any ideas for a workaround yet?

a-andreyev

a-andreyev commented on Dec 13, 2015

@a-andreyev

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

Toshik

Toshik commented on Dec 13, 2015

@Toshik
Contributor

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

andig

andig commented on Jan 13, 2016

@andig
Contributor

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

9 remaining items

igrr

igrr commented on Mar 11, 2016

@igrr
Member

Implemented in #1752

torntrousers

torntrousers commented on Mar 11, 2016

@torntrousers
Contributor

Woohoo, thank you!

added this to the 2.4.0 milestone on Jun 23, 2016
igrr

igrr commented on Jun 23, 2016

@igrr
Member

Merged and available in git version.

skorokithakis

skorokithakis commented on Aug 14, 2016

@skorokithakis
Contributor

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"...

electronicsguy

electronicsguy commented on Apr 6, 2017

@electronicsguy

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

bebo-dot-dev

bebo-dot-dev commented on Apr 6, 2017

@bebo-dot-dev

please see #3120 (comment)

electronicsguy

electronicsguy commented on Apr 6, 2017

@electronicsguy

@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 👍

AnishDey27

AnishDey27 commented on Jul 19, 2021

@AnishDey27

With arduino IDE while using NODEMCU variables are defined as 32 bit ( only "int" ) may be anyone defined it as int16_t or anything else. I used 32 bit and that solved my problem with sscanf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @skorokithakis@Toshik@andig@bebo-dot-dev@Links2004

        Issue actions

          sscanf compilation error · Issue #488 · esp8266/Arduino