The problem is the following:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float f = 0.0f;
int n = 0;
n = fscanf(stdin, "%f", &f);
printf("n = %d, f = %f\n", n, f);
return 0;
}
It prints:
n = 1, f = 100.0000
If the input string is:
100ergs
has been supplied to stdin
. The following behavior occurs on gcc (4.8.1) and VS2010 (and lower). Is this a bug, or am I missing something here? Because c standard (c89) in sections 7.19.6.2.19 and 7.19.6.2.20 clearly states that n should be equal to zero due to a matching failure.
UPD. just some additional info:
1) example from standard:
http://port70.net/~nsz/c/c99/n1256.html#7.19.6.2p20 (thx to Chris Culter for link)
2) similar example for matching failure which works as intended:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int hex = 0x0;
int n = 0;
n = fscanf(stdin, "%x", &hex);
printf("n = %d, hexVal = %x\n", n, hex);
return 0;
}
if stdin contains 0xz output is
n = 0, hexVal = 0