This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: builtin sscanf


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]