gcc warning

P

pemo

sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?
 
M

Mabden

pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

Because it is wrong. Now go and read a book or a help file or something
and LEARN why this is wrong. You will not grow up to be a programmer by
whinging (or whining) on about this to the Internet Magic Fix-It people.
 
E

Eric Sosman

pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

What's wrong: The effect of the `0' flag is not defined
for the "%s" conversion. If you call sprintf() this way,
the C Standard throws up its hands and refuses to say anything
about what your program might do. "The behavior is undefined."

Why the warning: Left as an exercise.
 
P

pemo

Now go and read a book or a help file or something

I have, and am continuing in my attempts to locate a reference that might
explain this: I would also LEARN if someone that already knows the 'why' of
this would simply tell me of course.

One reference I have found suggests doing this!!

sprintf(buff, "%s", "%05s");

sprintf(buffer, buff, "42");

That's just hiding the problem [what ever it is] from the compiler of
course.

I've also tried this using MS's cl, and intel's icl compilers - neither
complains about this on their highest warning levels.


Mabden said:
pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

Because it is wrong. Now go and read a book or a help file or something
and LEARN why this is wrong. You will not grow up to be a programmer by
whinging (or whining) on about this to the Internet Magic Fix-It people.
 
U

Ulrich Eckhardt

pemo said:
One reference I have found suggests doing this!!

sprintf(buff, "%s", "%05s");

sprintf(buffer, buff, "42");

One thing to ask yourself is what this is supposed to do.
That's just hiding the problem [what ever it is] from the compiler of
course.

Right. The compiler can't infer the format-string for the second sprintf()
call, so it can't check that it and the arguments make sense.
I've also tried this using MS's cl, and intel's icl compilers - neither
complains about this on their highest warning levels.

Which only means that their error-checking is worse. As pointed out, you
invoke UB and that doesn't require a diagnostic.

Please trim your quotes, too.

Uli
 
W

William Ahern

pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

If you're on a Unix system try:

man sprintf

What are you expecting '%05s" to do? Specifically, why are you specifying
"0" as one of the format flags? (Of course, that question just might give
you the answer.)
 
K

Keith Thompson

William Ahern said:
pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

If you're on a Unix system try:

man sprintf

What are you expecting '%05s" to do? Specifically, why are you specifying
"0" as one of the format flags? (Of course, that question just might give
you the answer.)

The semantics of sprintf(buffer, "%05s", "42") seem fairly clear -- or
rather, it seems clear what the semantics *would* be if they were
defined. Padding a string (rather than a number) with leading '0's
just isn't a common enough operation for it to be worth standardizing.
You might regret the lack if you happen to need to pad a string with
'0's, but next time you might need to pad it with some other arbitrary
character, and sprintf() isn't intended to solve all possible
problems. Sometimes you just have to roll your own solution.
 
P

pemo

Thanks Keith, esp. for answering in a non-cryptic, and helpful fashion.

I found the section in the c99 std which covers this myself yesterday - so,
I discovered that the semantics are undefined. Still, I think I'll at least
ask the gcc ppl if they could add some of the 'why' to such warnings: after
all, they've done the hard work testing for such things, so it seems only
fair that they should consider telling the user why they've included such a
check. But, perhaps this is implied, i.e., once one sees something like
this, one should consider all such warnings in the same light. There is one
other possibility - that some of the gcc folk are like some of the folks
here (although I very much doubt that)

Keith Thompson said:
William Ahern said:
pemo said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?

If you're on a Unix system try:

man sprintf

What are you expecting '%05s" to do? Specifically, why are you specifying
"0" as one of the format flags? (Of course, that question just might give
you the answer.)

The semantics of sprintf(buffer, "%05s", "42") seem fairly clear -- or
rather, it seems clear what the semantics *would* be if they were
defined. Padding a string (rather than a number) with leading '0's
just isn't a common enough operation for it to be worth standardizing.
You might regret the lack if you happen to need to pad a string with
'0's, but next time you might need to pad it with some other arbitrary
character, and sprintf() isn't intended to solve all possible
problems. Sometimes you just have to roll your own solution.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
K

Keith Thompson

pemo said:
Keith Thompson said:
William Ahern said:
sprintf(buffer, "%05s", "42");

[Warning] `0' flag used with `%s' printf format

What's wrong with this, like why the warning?
[snip]
The semantics of sprintf(buffer, "%05s", "42") seem fairly clear -- or
rather, it seems clear what the semantics *would* be if they were
defined. Padding a string (rather than a number) with leading '0's
just isn't a common enough operation for it to be worth standardizing.
You might regret the lack if you happen to need to pad a string with
'0's, but next time you might need to pad it with some other arbitrary
character, and sprintf() isn't intended to solve all possible
problems. Sometimes you just have to roll your own solution.
[...]
Thanks Keith, esp. for answering in a non-cryptic, and helpful fashion.

I found the section in the c99 std which covers this myself
yesterday - so, I discovered that the semantics are undefined.
Still, I think I'll at least ask the gcc ppl if they could add some
of the 'why' to such warnings: after all, they've done the hard work
testing for such things, so it seems only fair that they should
consider telling the user why they've included such a check. But,
perhaps this is implied, i.e., once one sees something like this,
one should consider all such warnings in the same light. There is
one other possibility - that some of the gcc folk are like some of
the folks here (although I very much doubt that)

Please don't top-post. Your reply goes below any quoted text, which
should be trimmed to what's relevant. In particular, it's rarely a
good idea to quote a signature. (Corrected.)

I'm not sure what additional information could usefully be added to
the warning message. It warns you that you've used a '0' flag with a
'%s' format. It seems obvious (to me, at least) that it's doing do
because a '0' flag isn't allowed with a '%s' format. The solution is
equally simple: don't do that.

What information would you add, that would fit in one line, that would
make the warning clearer?
 
E

Emmanuel Delahaye

pemo wrote on 24/09/05 :
There is one
other possibility - that some of the gcc folk are like some of the folks here
(although I very much doubt that)

Despite your whines, people here *are* helpful. They have pushed you to
move your ass so that you have been reading the standard to see what
exactly the story is. In other words, you have made a big step.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
K

Kenny McCormack

pemo wrote on 24/09/05 :

Despite your whines, people here *are* helpful. They have pushed you to
move your ass so that you have been reading the standard to see what
exactly the story is. In other words, you have made a big step.

Funny.
 
P

pemo

Please don't top-post. Your reply goes below any quoted text, which
should be trimmed to what's relevant. In particular, it's rarely a
good idea to quote a signature. (Corrected.)

'top post' - you mean post a reply on top of a previous reply?

That seems a bit weird IMHO (what's the reason?) - but, if it's the UseNet
'thing', will do.
What information would you add, that would fit in one line, that would
make the warning clearer?

Well, maybe it's just me, but I should have thought that warnings could be
aurmented :

Warning - implementation specific
Warning - non standard use

Or some such [better] wording perhaps.
 
P

pemo

Despite your whines, people here *are* helpful. They have pushed you to
move your ass so that you have been reading the standard to see what
exactly the story is. In other words, you have made a big step.

Sorry, but it was not meant as a whine - as far as I'm concerned, it's just
my truthful opinion: IMHO some people are helpful, and at the same time help
to impart new knowledge, but others are very less so; esp. when they can't
[even] be bothered to give a hint ... e.g., 'read the standard' - no, they
just flame you for asking something in the first place.

peetm
 
K

Keith Thompson

pemo said:
'top post' - you mean post a reply on top of a previous reply?

That seems a bit weird IMHO (what's the reason?) - but, if it's the UseNet
'thing', will do.

Read <http://www.caliburn.nl/topposting.html>.

You should also provide attributions for any quoted text. Any decent
newsreader should do this for you.
What information would you add, that would fit in one line, that would
make the warning clearer?

Well, maybe it's just me, but I should have thought that warnings could be
aurmented :

Warning - implementation specific
Warning - non standard use

Or some such [better] wording perhaps.

You may be right. Writing good diagnostics is a challenging art, and
it can be difficult for someone who already knows what it means to
convey enough information to someone who doesn't.
 
M

Mark McIntyre

Well, maybe it's just me, but I should have thought that warnings could be
aurmented :

You may be right. However since warnings are compiler specific, you
really need to take this up with the compiler writers in a compiler
specific group, not in CLC
Warning - implementation specific
Warning - non standard use

Personally I've always been in favour of

Warning: using gets is just an error so don't do it. Crivens.
Warning: main returns an int, idiot.
 
J

Joe Wright

pemo said:
Despite your whines, people here *are* helpful. They have pushed you to
move your ass so that you have been reading the standard to see what
exactly the story is. In other words, you have made a big step.


Sorry, but it was not meant as a whine - as far as I'm concerned, it's just
my truthful opinion: IMHO some people are helpful, and at the same time help
to impart new knowledge, but others are very less so; esp. when they can't
[even] be bothered to give a hint ... e.g., 'read the standard' - no, they
just flame you for asking something in the first place.

peetm

And even this is a whine. This is an open forum. Ask your question (or
whatever) and see what responses you get. If they are helpful, you win!
It they are not helpful, too bad. Complaining that you didn't win is
whining. Try it at the Lottery and see what sympathy you get. :)
 
R

rayw

And even this is a whine. This is an open forum. Ask your question (or
whatever) and see what responses you get. If they are helpful, you win! It
they are not helpful, too bad. Complaining that you didn't win is whining.
Try it at the Lottery and see what sympathy you get. :)

And even this is a whine

LOL - I've never played the lottery - there are idiots, and there are *real*
idiots.
 
P

pemo

And even this is a whine. This is an open forum. Ask your question (or
whatever) and see what responses you get. If they are helpful, you win! It
they are not helpful, too bad. Complaining that you didn't win is whining.
Try it at the Lottery and see what sympathy you get. :)

--

As I said - just IMHO
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top