-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Closed
Milestone
Description
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.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
Select code repository
Activity
chadouming commentedon Jun 30, 2015
You need os_scanf and not os_sprintf
On Mon, Jun 29, 2015, 7:04 PM nicjohnston notifications@github.com wrote:
nicjohnston commentedon Jun 30, 2015
Thanks for the reply, however I just tried os_scanf, and it threw the following error
chadouming commentedon Jun 30, 2015
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 commentedon Jun 30, 2015
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 commentedon Jun 30, 2015
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 commentedon Jun 30, 2015
String is a class not a type.
warning free:
nicjohnston commentedon Jun 30, 2015
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 commentedon Aug 12, 2015
completely agree with @nicjohnston, we should implement
siscanf(...)
Toshik commentedon Oct 23, 2015
Any news about sscanf?
torntrousers commentedon Oct 25, 2015
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 commentedon Dec 11, 2015
any ideas for a workaround yet?
a-andreyev commentedon Dec 13, 2015
@lighthousebulb, I guess current ugly workaround is to use Arduino String.
Toshik commentedon Dec 13, 2015
@a-andreyev How do you suppose to use String instead of sscanf?
andig commentedon Jan 13, 2016
+1 for
sscanf
as its available on "standard" Arduino.9 remaining items
igrr commentedon Mar 11, 2016
Implemented in #1752
torntrousers commentedon Mar 11, 2016
Woohoo, thank you!
igrr commentedon Jun 23, 2016
Merged and available in git version.
skorokithakis commentedon Aug 14, 2016
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 commentedon Apr 6, 2017
@igrr Has sscanf() been implemented in the latest build? I still get
undefined reference to sscanf
error.bebo-dot-dev commentedon Apr 6, 2017
please see #3120 (comment)
electronicsguy commentedon Apr 6, 2017
@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 commentedon Jul 19, 2021
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