473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

gcc warning

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

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

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

Nov 15 '05 #1
19 9102
"pemo" <us***********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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.

--
Mabden
Nov 15 '05 #2
pemo wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
>>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" <mabden@sbc_global.net> wrote in message
news:0A*****************@newssvr13.news.prodigy.co m...
"pemo" <us***********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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.

--
Mabden

Nov 15 '05 #4
pemo wrote:
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

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !

Nov 15 '05 #5

"Ulrich Eckhardt" <do******@knuut.de> wrote in message
news:3p************@uni-berlin.de...
....
... As pointed out, you
invoke UB and that doesn't require a diagnostic.


invoke UB?
Nov 15 '05 #6
Oh, google - 'undefined behaviour'.
Nov 15 '05 #7
pemo <us***********@gmail.com> wrote:
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.)
Nov 15 '05 #8
William Ahern <wi*****@wilbur.25thandClement.com> writes:
pemo <us***********@gmail.com> wrote:
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) ks***@mib.org <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.
Nov 15 '05 #9
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" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
William Ahern <wi*****@wilbur.25thandClement.com> writes:
pemo <us***********@gmail.com> wrote:
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) ks***@mib.org
<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.

Nov 15 '05 #10
"pemo" <us***********@gmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
William Ahern <wi*****@wilbur.25thandClement.com> writes:
pemo <us***********@gmail.com> wrote:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #11
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
Nov 15 '05 #12
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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.


Funny.

Nov 15 '05 #13
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.
Nov 15 '05 #14
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
Nov 15 '05 #15
"pemo" <us***********@gmail.com> writes:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #16
On Mon, 26 Sep 2005 08:30:43 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #17
pemo wrote:
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. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #18

"Joe Wright" <jw*****@comcast.net> wrote in message
news:e-********************@comcast.com...
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.
Nov 15 '05 #19

"Joe Wright" <jw*****@comcast.net> wrote in message
news:e-********************@comcast.com...

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
Nov 15 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
3
by: Bill Burris | last post by:
How do I find what is causing this warning from the Linker? If I use /NODEFAULTLIB I get hundreds of undefined symbols. LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
1
by: spanov | last post by:
i've got problem installing python-2.3.5 from sources on FreeBSD 5.3 root@server# ./configure > conf_log configure: WARNING: curses.h: present but cannot be compiled configure: WARNING:...
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
5
by: holmescn | last post by:
what is the meaning of warning attributes ignored on template instantiation. i got it when i compiled stlport 5.1.3. anybody can help me ? thx!
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.