0

I need to convert recursively char[] into float variable. My code is as follows:

while(1)
{
    //take char[] str as input
    sscanf(str,"%f",&n);
    //printf n
}

The program works well for positive numbers. But if we give negative numbers initially followed by positive numbers then n is displaying different output.

4

1 回答 1

1

用C写的,这似乎对我有用。

char* str = "-9 1.1 2 3.1 0.9 .8 -2 -1.1";
float n;
int offset;
while (sscanf(str, "%f%n", &n, &offset) > 0) {
  printf("%f,", n);
  str += offset;
}

与输出-9.000000,1.100000,2.000000,3.100000,0.900000,0.800000,-2.000000,-1.100000,

于 2013-10-03T13:44:41.023 回答