assignment suppression in sscanf

B

Bart Vandewoestyne

I noticed that the following line

sscanf(buf, "%*d %lf %lf %*lf", VX+n, VY+n);

leads to the following gcc warning:

warning: use of assignment suppression and length modifier
together in gnu_scanf format

I would assume this is due to the %*lf format specifier. To
solve the problem, i noticed that changing the line to

sscanf(buf, "%*d %lf %lf %*f", VX+n, VY+n);

makes the warning go away. It appears that %*lf is something
'bad' to use, but why? Is my solution indeed the best way to get
rid of this warning and make my code more C89 compliant?

Thanks,
Bart
 
E

Ersek, Laszlo

I noticed that the following line

sscanf(buf, "%*d %lf %lf %*lf", VX+n, VY+n);

leads to the following gcc warning:

warning: use of assignment suppression and length modifier
together in gnu_scanf format

I would assume this is due to the %*lf format specifier. To solve the
problem, i noticed that changing the line to

sscanf(buf, "%*d %lf %lf %*f", VX+n, VY+n);

makes the warning go away. It appears that %*lf is something 'bad' to
use, but why? Is my solution indeed the best way to get rid of this
warning and make my code more C89 compliant?

It is not something "bad", but a mild conflict of interest. The conversion
specifier character "f" tells sscanf() how a matching input sequence must
look like. The length modifier "l" specifies the size of the receiving
object (which, in case of "f", specifies range and precision). The "*"
assignment-suppressing character tells sscanf() to parse (validate) the
input sequence, but not to try to convert it. Since you don't want to
store anything, specifying the size of the (nonexistent) receiving object
is useless.

gcc warns you to clarify what you want sscanf() to do. I won't try to
determine whether the standard allows the "%*lf" directive (superficially,
I think it does), but "%*f" seems better style.

Cheers,
lacos
 
E

Eric Sosman

I noticed that the following line

sscanf(buf, "%*d %lf %lf %*lf", VX+n, VY+n);

leads to the following gcc warning:

warning: use of assignment suppression and length modifier
together in gnu_scanf format

I would assume this is due to the %*lf format specifier. To
solve the problem, i noticed that changing the line to

sscanf(buf, "%*d %lf %lf %*f", VX+n, VY+n);

makes the warning go away. It appears that %*lf is something
'bad' to use, but why? Is my solution indeed the best way to get
rid of this warning and make my code more C89 compliant?

The code conforms, either with or without the "l" modifier,
and does what you want: It matches and converts a floating-point
number, ignores it, and moves onward (in this case, to the end
of the format string).

However, the "l" is largely meaningless. If it were not for
the "*", the "l" would mean "the corresponding argument is a pointer
to double, so store the converted value as a double (not as a float
or as a long double)." Because of the "*", though, there *is* no
corresponding argument for the "l" to describe, and no point in
trying to describe it. You've told us you have no sister, and also
mentioned that she's blonde.

Presumably, gcc thinks it odd -- not wrong, necessarily, but
odd -- to specify so much detail about an argument that doesn't
exist. gcc generates a message to draw your attention to the
oddity, in case there really is something wrong with it -- maybe
the "*" should actually have been on a different conversion, for
example. gcc generates messages for other perfectly legal but
suspicious constructs like `if (x = y)', in a similar vein.
 
B

Ben Bacarisse

Bart Vandewoestyne said:
I noticed that the following line

sscanf(buf, "%*d %lf %lf %*lf", VX+n, VY+n);

leads to the following gcc warning:

warning: use of assignment suppression and length modifier
together in gnu_scanf format

I would assume this is due to the %*lf format specifier. To
solve the problem, i noticed that changing the line to

sscanf(buf, "%*d %lf %lf %*f", VX+n, VY+n);

makes the warning go away. It appears that %*lf is something
'bad' to use, but why?

This is not a required diagnostic; gcc is just being helpful. Nothing
about %*lf is "wrong" in the sense of being a constraint violation or
invoking undefined behaviour (unless I've missed something, of course).

I think the reason gcc chooses to tell you this is that the length
modifier (l in this case) has no effect of what constitutes a valid
input sequence. As a result, %*f and %*lf mean exactly the same thing
so there is some merit in telling you that the format string is
over-specified.
Is my solution indeed the best way to get
rid of this warning and make my code more C89 compliant?

I think your code is fine with or without this change but I would remove
the 'l' simply to clean up the compiler's ouptut.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top