Re: builtin sscanf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux