关于C#:sscanf在以字符n或i开头的字符串中搜索float时返回1

sscanf returns 1 when searching for float in a string starting with the character n or i

在下面的代码中,我希望sscanf返回0,但它返回1并将0.000000分配给float变量x。 当字符串以字母i开头但没有其他字母时,会发生相同的行为。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main() {
    int ss_return;
    float x;
    char str_to_sscanf[] ="noduh";

    ss_return = sscanf( str_to_sscanf,"%f", &x );

    printf("

The word passed to sscanf is %s"
, str_to_sscanf );
    printf("

When looking for a float, sscanf returned %d"
, ss_return );
    printf("

and assigned %f to x (declared as float)"
, x );
    printf("

WHY DID sscanf NOT RETURN ZERO????"
);
}

该程序的输出为:"两个看起来很奇怪的字符"

我想念什么?


我的猜测是您的库的状态机将" n"视为" NaN"的可能开始,而将" i"视为" inf"的可能开始,这使其陷入了float转换代码中。 然后由于无法完成解析并立即分配默认值,它立即退出工作。