I try to separate and parse two numbers out of a string, one is a float and one is an int. So I do this...

char packet_buffer[24];
float dist_float;
uint32_t quality;
uint32_t distance;
strncpy(packet_buffer, ": 2.846m,0089", 24); 
memmove(packet_buffer, packet_buffer+1, strlen(packet_buffer)); //Remove leading colon
sscanf(packet_buffer, "%fm,%d", &dist_float, &quality);
distance = (uint32_t)(dist_float*1000);

This returns 96 for quality and 0 for distance... but running it with Ideone works. Did I overlook something in combination with the ATMega?

I'd expect 89 for quality and 2846 for distance.

Edit: After adding the build parameters suggested by Filo, the output looks like this

    "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega168p -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega168p" -c -gdwarf-2 -std=gnu99 -ffreestanding -fdata-sections  -Wl,-u,vfscanf -lscanf_flt -lm -MD -MP -MF "src/main.d" -MT"src/main.d" -MT"src/main.o"   -o "src/main.o" "../src/main.c" 
    Finished building: ../src/main.c
  • \$\begingroup\$ Have you configured the standard library so that it can handle those things? In a micro there is usually a much less capable library especially for things like string parsing. Usually there is a compiler option but I don't work with ATMegas, so can't tell you details. \$\endgroup\$ – Arsenal May 30 '18 at 7:03
  • \$\begingroup\$ "This returns 96 for quality and 0 for distance...", and what do you expect? 100 for quality and 2.846 for distance? \$\endgroup\$ – Harry Svensson May 30 '18 at 7:03
  • \$\begingroup\$ @Arsenal: You mean configure to use the sscanf function? Where would I do that? @Harry: I'd expect 89 for quality and 2846 for distance \$\endgroup\$ – po.pe May 30 '18 at 7:08
  • \$\begingroup\$ Do you printf the variables or inspect with a debugger? If printf then please see my updated answer. \$\endgroup\$ – filo May 30 '18 at 8:31
  • 1
    \$\begingroup\$ If you find yourself using float numbers and stdio.h on a 8-bit kitchen sink MCU, you are doing it terribly wrong. Things to study: how C code translates to machine code, what is a FPU, fixed point arithmetic. Also strncpy is a dangerous function that should be avoided. \$\endgroup\$ – Lundin May 30 '18 at 11:18

AVR libc has stdio functions in two flavors, one small (without floats) and another full-size with all features. See for example the description of vscanf from AVR libc, basically you have to build with:

-Wl,-u,vfscanf -lscanf_flt -lm

And if you want to printf the variables you also have to add:

-Wl,-u,vfprintf -lprintf_flt -lm
| improve this answer | |
  • \$\begingroup\$ Any chance you know how to do this in AtmelStudio 7? \$\endgroup\$ – po.pe May 30 '18 at 7:11
  • \$\begingroup\$ "atmel studio compiler flags" microchip.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/… \$\endgroup\$ – filo May 30 '18 at 7:13
  • \$\begingroup\$ So I added those flags to the Misc flags of the AVR/GNU C Compiler but the outcome is the same. Do I have to put it somewhere else? \$\endgroup\$ – po.pe May 30 '18 at 7:23
  • \$\begingroup\$ First clean and fully rebuild the project, if it does not help, then best if you post the whole output of your build process, there should be a console or something with gcc commands and their outputs. \$\endgroup\$ – filo May 30 '18 at 7:27
  • \$\begingroup\$ See my edit in the start post... \$\endgroup\$ – po.pe May 30 '18 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.