Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: A printf question. |
| Date | 2015-10-13 23:15 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <mvjs6j$3f6$1@dont-email.me> (permalink) |
| References | <mvj3b8$i7h$1@dont-email.me> <a86c606e-3da8-4561-9b40-2d5d8887fc3c@googlegroups.com> <ln37xebr27.fsf@kst-u.example.com> |
On 13/10/15 19:45, Keith Thompson wrote:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>> On Tuesday, October 13, 2015 at 3:12:07 PM UTC+1, Mehdi Amini Valashani son of Bahram wrote:
>>> // Following a discussion on a forum, I got this code snippet:
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> int main(void)
>>> {
>>> const char * const message="My str\n";
>>>
>>> //printf("%s", message); // This seems to be correct.
>>> printf(message); // Should not this produce a diagnostic?
>>>
>>> return EXIT_SUCCESS;
>>> }
>>>
>> No, its normally a bad idea to pass a string composed on the fly as
>> the format argument to printf (largely because you might have embedded
>> percent signs in it), but it's not prohibited, and occasionally it's
>> the right thing to do. printf("%s", message) is the normal way of
>> printing out a string whose contents aren't known at the point of
>> making the call, however.
>
> Either that or fputs(message, stdout), or puts(message) if you want to
> add a newline.
>
> printf format strings are typically literals, which are safe *if* the
> code is written carefully and correctly. A format string can be
> generated at run time by some other means, but you *must* be careful not
> to include '%' characters that you didn't intend to. Using unchecked
> user input as a format string is arbitrarily dangerous.
>
Using a literal string for printf also means that your compiler may be
able to do extra checking, to ensure that % characters in the format
match up with other arguments passed to printf. (gcc can certainly do
that, given the write options. And it translates "printf("%s",
message);" into a puts() call - removing the extra "\n". Write your
code in the clearest and safest manner, and let the compiler generate
the most efficient object code.)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A printf question. MehdiAmini <atorrses@gmail.com> - 2015-10-13 14:09 +0000
Re: A printf question. Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-13 10:46 -0400
Re: A printf question. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-13 09:19 -0700
Re: A printf question. Keith Thompson <kst-u@mib.org> - 2015-10-13 10:45 -0700
Re: A printf question. David Brown <david.brown@hesbynett.no> - 2015-10-13 23:15 +0200
Re: A printf question. James Kuyper <jameskuyper@verizon.net> - 2015-10-13 15:25 -0400
Re: A printf question. glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-14 02:55 +0000
Re: A printf question. James Kuyper <jameskuyper@verizon.net> - 2015-10-13 23:12 -0400
Re: A printf question. supercat@casperkitty.com - 2015-10-14 06:34 -0700
Re: A printf question. Keith Thompson <kst-u@mib.org> - 2015-10-14 10:26 -0700
Re: A printf question. MehdiAmini <atorrses@gmail.com> - 2015-10-15 14:06 +0000
Re: A printf question. James Kuyper <jameskuyper@verizon.net> - 2015-10-15 10:26 -0400
Re: A printf question. Noob <root@127.0.0.1> - 2015-10-25 23:02 +0100
csiph-web