Explain this sscanf behavior.

Nick Mason mason at tc.fluke.COM
Sat Jul 7 04:18:30 AEST 1990





What should sscanf do with the following?  Does anyone
have the ANSI standard and shed some light on the following?

I would like "hard" replies, not "I think it should ....".

Thanks in advance.

Given:

	char *buf="123";
	char *str="123x";

	int  a, b, x;

	b = -99;

	x = sscanf(str, "%d%n", &a, &b);

	printf("x=%d, a=%d, b=%d\n",x,a,b);

	x = sscanf(buf, "%d%n", &a, &b);

	printf("x=%d, a=%d, b=%d\n",x,a,b);


What is the CORRECT output according to the standard???

I tried this with 3 different compilers and got the following:

compiler A:

	x=1   a=123  b=3
	x=1   a=123  b=3

compiler B:

	x=1   a=123  b=3
	x=1   a=123  b=4  <-- yes 4.

compiler C:

	x=1  a=123  b=3
	x=1  a=123  b= -99



I'm confused????!!!!! Compiler C is "100% ANSI compatible".????


Nick.



More information about the Comp.std.c mailing list