Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72727

Re: A printf question.

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: A printf question.
Date 2015-10-13 10:45 -0700
Organization None to speak of
Message-ID <ln37xebr27.fsf@kst-u.example.com> (permalink)
References <mvj3b8$i7h$1@dont-email.me> <a86c606e-3da8-4561-9b40-2d5d8887fc3c@googlegroups.com>

Show all headers | View raw


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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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