Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fputs and fprintf

8 views
Skip to first unread message

_JusSx_

unread,
Feb 28, 2010, 3:26:15 AM2/28/10
to
Hi,
I don't know if the question is in newsgroup FAQ because I haven't found
it yet and so I haven't read it yet.

I would like to know what differences are between these two
C functions: *fputs* and *fprintf*.

fputs C function has been used much in coreutils C source code while fprintf
has been used to printf help or error.

$ grep fputs *.c

#v+
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: if (fputs ("\n", out) < 0)
base64.c: if (wrap_column && current_column > 0 && fputs ("\n", out) < 0)
basename.c: fputs (_("\
basename.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
basename.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
cat.c: fputs (_("\
cat.c: fputs (_("\
cat.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
cat.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
cat.c: fputs (_("\
chcon.c: fputs (_("\
...
#v-

#v+
$ grep fprintf *.c

base64.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
basename.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
cat.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
chcon.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
chgrp.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
...
#v-

Does using one function depend on only programmer's taste?


Thanks in advance

-JusSx-

--
Linux is only free if your time has no value

santosh

unread,
Feb 28, 2010, 3:43:30 AM2/28/10
to
_JusSx_ <jussx0...@gmail.com.invalid> writes:

> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't
> found it yet and so I haven't read it yet.

Google could've helped you...

<http://c-faq.com/>

> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results. You can use it to print the values of nearly all of C's
object types.

fputs simply writes the string you supply it to the indicated output
stream.

<http://www.dinkumware.com/manuals/?manual=compleat&page=stdio.html>
<http://www.dinkumware.com/manuals/?manual=compleat&page=lib_over.html>

and man fprintf/fputs on your system too.

> Does using one function depend on only programmer's taste?

On the programmer's requirements, more than taste. fprintf is the way
to go for writing out the values of the various supported types,
unless you have your own version. fputs doesn't do that, but it's
fine for simply writing C strings.


Seebs

unread,
Feb 28, 2010, 3:46:20 AM2/28/10
to
On 2010-02-28, _JusSx_ <jussx0...@gmail.com.invalid> wrote:
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

One of them formats output, one prints a string without formatting it.

> Does using one function depend on only programmer's taste?

No.

Have you considered the idea of getting some kind of documentation, book,
or anything like that? If "man fprintf" doesn't tell you anything, your
system is misconfigured. (I'm assuming something unixy because you're
looking at coreutils.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Richard Heathfield

unread,
Feb 28, 2010, 3:54:48 AM2/28/10
to
_JusSx_ wrote:
> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fputs writes a string to the output stream, and you have to know the
string up front; whereas the data that fprintf writes can be built at
run-time:

Contrast:

fputs("now is the time for all good men to party\n", stderr);

with:

fprintf(stderr,
"now is the time for %d good men to party\n",
goodmenqty);

<snip>

> Does using one function depend on only programmer's taste?

No, they have different purposes. If you just want to write a single
fixed string to the output stream, fputs can do this quickly and without
fuss. But if your data needs are more complex, fputs isn't up to it, and
that's the time to look at fprintf.

(Some people just use fprintf for everything and be done with it. That's
a perfectly workable strategy.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

_JusSx_

unread,
Feb 28, 2010, 5:09:46 AM2/28/10
to
On 2010-02-28, santosh <santo...@gmail.com> wrote:
> _JusSx_ <jussx0...@gmail.com.invalid> writes:
>
>> Hi,
>> I don't know if the question is in newsgroup FAQ because I haven't
>> found it yet and so I haven't read it yet.
>
> Google could've helped you...
>
><http://c-faq.com/>
>
> ...

Thank you very much. I have bookmarked c-faq link. I will give a look
at it.

_JusSx_

unread,
Feb 28, 2010, 5:08:32 AM2/28/10
to
On 2010-02-28, Seebs <usenet...@seebs.net> wrote:
> On 2010-02-28, _JusSx_ <jussx0...@gmail.com.invalid> wrote:
>> I would like to know what differences are between these two
>> C functions: *fputs* and *fprintf*.
>
> One of them formats output, one prints a string without formatting it.
>
>> Does using one function depend on only programmer's taste?
>
> No.
>
> Have you considered the idea of getting some kind of documentation, book,
> or anything like that? If "man fprintf" doesn't tell you anything, your
> system is misconfigured. (I'm assuming something unixy because you're
> looking at coreutils.)
>
> -s

Yes, I read fputs and fprintf man pages but maybe I didn't read them
carefully.

Now I know the difference is: fprintf prints formatted output, fputs
doesn't print formatted output.

I thought fputs printed formatted output like fprint does but I was
wrong.

pete

unread,
Feb 28, 2010, 6:13:34 AM2/28/10
to

fputs is a much simpler function than fprintf is.
I once advised a colleague that he might be able to shrink his code size
if he was able to replace all of his fprintf calls with fputs calls.
It worked, which was good because at that time
he did need to shrink his code size.

--
pete

bartc

unread,
Feb 28, 2010, 7:21:10 AM2/28/10
to
"_JusSx_" <jussx0...@gmail.com.invalid> wrote in message
news:slrn.201002...@news.eternal-september.org...

> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fputs/puts prints a string, while fprintf/printf prints a (format) string
*and* any number of other values.

But one important difference is that puts writes a newline at the end, while
printf requires you to insert the fiddly \n sequence at the end of the
string (significant for terrible typists like me, with \ being one of those
keys that is in a different place on every keyboard).

--
Bartc

Nick Keighley

unread,
Feb 28, 2010, 7:46:18 AM2/28/10
to
On 28 Feb, 08:26, _JusSx_ <jussx0NOS...@gmail.com.invalid> wrote:


> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

<snip>

the FAQ is at
http://c-faq.com/

but the FAQ won't help with your question, you need a C library
reference for that
http://www.dinkumware.com/manuals/#Standard C Library

pete

unread,
Feb 28, 2010, 8:00:43 AM2/28/10
to
bartc wrote:

> fputs/puts prints a string,
> while fprintf/printf prints a (format) string
> *and* any number of other values.
>
> But one important difference is that
> puts writes a newline at the end, while
> printf requires you to insert the fiddly \n sequence at the end of the
> string (significant for terrible typists like me,
> with \ being one of those
> keys that is in a different place on every keyboard).

Another important difference is that
puts writes a newline at the end, while fputs doesn't.

--
pete

bartc

unread,
Feb 28, 2010, 9:10:43 AM2/28/10
to

"pete" <pfi...@mindspring.com> wrote in message
news:4B8A68...@mindspring.com...

No? That's crazy then, why make fputs() and puts() behave differently.

--
Bartc

Message has been deleted

pete

unread,
Feb 28, 2010, 9:32:31 AM2/28/10
to

I don't know why.

N869
7.19.7.4 The fputs function
Synopsis
[#1]
#include <stdio.h>
int fputs(const char * restrict s,
FILE * restrict stream);
Description
[#2] The fputs function writes the string pointed to by s to
the stream pointed to by stream. The terminating null
character is not written.

--
pete

bartc

unread,
Feb 28, 2010, 10:00:27 AM2/28/10
to
"pete" <pfi...@mindspring.com> wrote in message
news:YoidnbsSb57n4xfW...@earthlink.com...

> bartc wrote:
>>
>> "pete" <pfi...@mindspring.com> wrote in message
>> news:4B8A68...@mindspring.com...
>>
>>> bartc wrote:
>>>
>>>> fputs/puts prints a string,
>>>> while fprintf/printf prints a (format) string
>>>> *and* any number of other values.
>>>>
>>>> But one important difference is that
>>>> puts writes a newline at the end, while
>>>> printf requires you to insert the fiddly \n sequence at the end of the
>>>> string (significant for terrible typists like me,
>>>> with \ being one of those
>>>> keys that is in a different place on every keyboard).
>>>
>>>
>>> Another important difference is that
>>> puts writes a newline at the end, while fputs doesn't.
>>
>>
>> No? That's crazy then, why make fputs() and puts() behave differently.
>
> I don't know why.

> [#2] The fputs function writes the string pointed to by s to


> the stream pointed to by stream. The terminating null
> character is not written.

I vaguely remember now fputs/puts had to be compatible with fputs/gets(),
which retain/don't retain a newline character.

Still, you would have expected fputs/puts to do the same thing other than
one takes a file parameter and the other defaults to stdout.

--
Bartc

santosh

unread,
Feb 28, 2010, 10:02:06 AM2/28/10
to
bartc <ba...@freeuk.com> writes:

I'd guess that the reason was because while it makes sense to advance
to a newline after printing a line on the standard output (which is
most often a console), it makes less sense in a file.

And also, maybe for symmetry with gets/puts and fgets/fputs
combination. In each pair one reverses the behaviour of the other, in
terms of adding or discarding a newline.

And it's useful to have a function that can write strings without
adding characters on it's own. Sure fputc and fprintf are there, but
while the former is too primitive, the latter adds overhead for just
writing a string.

All the above just my naive guesses:)


Richard Heathfield

unread,
Feb 28, 2010, 12:51:09 PM2/28/10
to
Stefan Ram wrote:

> _JusSx_ <jussx0...@gmail.com.invalid> writes:
>> I would like to know what differences are between these two
>> C functions: *fputs* and *fprintf*.
>
> Remarkable: The answer I would expect to be most often
> did not occur at all so far (in the subset of the posters
> I read):
>
> fprintf( stderr, userinput );

That would be very, very silly - a bit like crossing the road with your
eyes shut.

> An attacker who can fully or partially control the contents
> of a format string can crash a vulnerable process, view the
> contents of the stack, view memory content, or write to an
> arbitrary memory location and consequently execute arbitrary
> code with the permissions of the vulnerable process [Seacord 05a].

Which is the reason you don't give attackers control of the format string.

>
> [Seacord 05a] Seacord, Robert C. Secure Coding in C and C++.
> Boston, MA: Addison-Wesley, 2005. See
> http://www.cert.org/books/secure-coding for news and errata.

Yeah. I started reviewing his stuff in considerable detail, but he
seemed to lose interest after the Nth crit (for large N).

0 new messages