# Extract numbers from String on ATMega168

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

• 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. – Arsenal May 30 '18 at 7:03
• "This returns 96 for quality and 0 for distance...", and what do you expect? 100 for quality and 2.846 for distance? – Harry Svensson May 30 '18 at 7:03
• @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 – Humpawumpa May 30 '18 at 7:08
• Do you printf the variables or inspect with a debugger? If printf then please see my updated answer. – filo May 30 '18 at 8:31
• 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. – 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

• Any chance you know how to do this in AtmelStudio 7? – Humpawumpa May 30 '18 at 7:11
• "atmel studio compiler flags" microchip.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/… – filo May 30 '18 at 7:13
• 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? – Humpawumpa May 30 '18 at 7:23
• 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. – filo May 30 '18 at 7:27
• See my edit in the start post... – Humpawumpa May 30 '18 at 8:12