builtin sscanf

Mason slash.tmp@free.fr
Thu Oct 20 11:33:00 GMT 2016


On 19/10/2016 20:45, fedor_qd wrote:

> How precise [is] this function?
> I write sample:
>
> int main()
>     {
>     float x, y;
>     __builtin_sscanf("30,981", "%f", &x);
>     __builtin_printf("Test comma: %f\n", x);
>     return(0);
>     }
> 
> Compile with options: g++ main.c -o hello -static-libgcc
> 
> This sample returns 30.00 but 30.981 expected

You passed "30,981" to scanf.

Are you sure ',' is a valid decimal mark in the current locale?
( https://en.wikipedia.org/wiki/Decimal_mark )

If not, the input is equivalent to "30&981", thus scanf will stop
parsing on the unexpected character.

#include <stdio.h>
int main(void)
{
  float x, y;
  sscanf("30,981", "%f", &x);
  sscanf("30.981", "%f", &y);
  printf("x=%f y=%f\n", x, y);
  return 0;
}

$ ./a.out 
x=30.000000 y=30.981001

Regards.



More information about the Gcc-help mailing list