Difference between fprintf, printf and sprintf?

244,252

Solution 1

In C, a "stream" is an abstraction; from the program's perspective it is simply a producer (input stream) or consumer (output stream) of bytes. It can correspond to a file on disk, to a pipe, to your terminal, or to some other device such as a printer or tty. The FILE type contains information about the stream. Normally, you don't mess with a FILE object's contents directly, you just pass a pointer to it to the various I/O routines.

There are three standard streams: stdin is a pointer to the standard input stream, stdout is a pointer to the standard output stream, and stderr is a pointer to the standard error output stream. In an interactive session, the three usually refer to your console, although you can redirect them to point to other files or devices:

$ myprog < inputfile.dat > output.txt 2> errors.txt

In this example, stdin now points to inputfile.dat, stdout points to output.txt, and stderr points to errors.txt.

fprintf writes formatted text to the output stream you specify.

printf is equivalent to writing fprintf(stdout, ...) and writes formatted text to wherever the standard output stream is currently pointing.

sprintf writes formatted text to an array of char, as opposed to a stream.

Solution 2

printf outputs to the standard output stream (stdout)

fprintf goes to a file handle (FILE*)

sprintf goes to a buffer you allocated. (char*)

Solution 3

  • printf(const char *format, ...) is used to print the data onto the standard output which is often a computer monitor.
  • sprintf(char *str, const char *format, ...) is like printf. Instead of displaying the formated string on the standard output i.e. a monitor, it stores the formated data in a string pointed to by the char pointer (the very first parameter). The string location is the only difference between printf and sprint syntax.
  • fprintf(FILE *stream, const char *format, ...) is like printf again. Here, instead of displaying the data on the monitor, or saving it in some string, the formatted data is saved on a file which is pointed to by the file pointer which is used as the first parameter to fprintf. The file pointer is the only addition to the syntax of printf.

If stdout file is used as the first parameter in fprintf, its working is then considered equivalent to that of printf.

Solution 4

printf(...) is equivalent to fprintf(stdout,...).

fprintf is used to output to stream.

sprintf(buffer,...) is used to format a string to a buffer.

Note there is also vsprintf, vfprintf and vprintf

Solution 5

printf

  1. printf is used to perform output on the screen.
  2. syntax = printf("control string ", argument );
  3. It is not associated with File input/output

fprintf

  1. The fprintf it used to perform write operation in the file pointed to by FILE handle.
  2. The syntax is fprintf (filename, "control string ", argument );
  3. It is associated with file input/output
Share:
244,252

Related videos on Youtube

Vishwanath Dalvi
Author by

Vishwanath Dalvi

SOreadytohelp about me box is kept "", intentionally.

Updated on June 28, 2020

Comments

  • Vishwanath Dalvi
    Vishwanath Dalvi almost 4 years

    Can anyone explain in simple English about the differences between printf, fprintf, and sprintf with examples?

    What stream is it in?

    I'm really confused between the three of these while reading about "File Handling in C".

    • Nawaz
      Nawaz over 13 years
      Experiment with them by writing some sample code. That is very easy way to clearly understand the difference between them.
    • Maxim Egorushkin
      Maxim Egorushkin over 13 years
      Prefer snprintf() to sprintf() to avoid silly buffer overflows.
    • John Dibling
      John Dibling over 13 years
      Prefer streams or Boost formatters to avoid silly buffer overflows and nasty type-unsafety bugs
    • Maxim Egorushkin
      Maxim Egorushkin over 13 years
      Streams and boost formatters may be too slow.
    • Moo-Juice
      Moo-Juice over 13 years
      @Maxim, whilst you raise a valid point I'll take the safety in knowledge that my buffers aren't going to overflow and explode my app in to pieces. I'd only ever look at these functions if the streams/boost formatters were shown to be causing noticeable bottlenecks. :)
    • Matthieu M.
      Matthieu M. over 13 years
      I've dropped the C tag, those are C functions and while you can call them from C++, it's better not to, this way you'll avoid grumpy programmers (in C++, use streams)
    • John Dibling
      John Dibling over 13 years
      @Maxim: Almost never. I use streams in production financial servers, processing 5 million messages per second, all day every day and they have never been an issue for me.
    • John Dibling
      John Dibling over 13 years
      @Matthieu: I think you actually dropped the [c++] tag. Was this intended?
    • Maxim Egorushkin
      Maxim Egorushkin over 13 years
      @John: type safety of streams is a bit overrated. Modern compilers check the arguments against the format string, just make sure you don't ignore these warnings or make them a hard error. And in the end of the day streams do call snprintf under the hood, so by not using streams you avoid layers of code.
    • Maxim Egorushkin
      Maxim Egorushkin over 13 years
      @John: 5M/sec messages is 200ns/msg. I bet you could do better than that if you drop using IOStreams. lol
    • John Dibling
      John Dibling over 13 years
      @Maxim: Point is, there's no need. My code isn't a bottleneck. 5m/sec is how many messages the exchange sends, not how many we're capable of processing. So why prematurely optimize?
    • Maxim Egorushkin
      Maxim Egorushkin over 13 years
      @John: With market data and high-frequency trading the goal is often not throughput, but rather latency.
    • John Dibling
      John Dibling over 13 years
      @Maxim: I'm well-aware of this. Again it's not an issue in my experience.
  • John Dibling
    John Dibling over 13 years
    The v flavors are non-standard i believe
  • Fred Foo
    Fred Foo over 13 years
    The v flavors are very certainly in the C standard.
  • John Dibling
    John Dibling over 13 years
    +1 useful indeed, but be aware that vsnprintf is a non-Standard function. Most implementations I've seen implement this or something like it, but it is implementation-specific.
  • John Dibling
    John Dibling over 13 years
    @larsmans: ah, ok. thanks for the correction. This question was at one point tagged [c++]
  • John Dibling
    John Dibling over 13 years
    @larsmans mentions above that this is, in fact, a part of the C standard, so I take that back
  • onemasse
    onemasse over 13 years
    What does this have to do with C?
  • SuperBiasedMan
    SuperBiasedMan almost 9 years
    If this is quoted from somewhere else it's best to cite the source with a link, but definitely still keep the text you've quoted here.
  • Dave Voyles
    Dave Voyles about 7 years
    "it is simply a producer (input stream) or consumer (output stream) of bytes." Are these backwards? Wouldn't a producer create (output) something? Asking because I genuinely do not know.
  • John Bode
    John Bode about 7 years
    @DaveVoyles: These are from the perspective of your program. An input stream produces bytes for your program to read; an output stream consumes the bytes produced from your program.
  • mfaani
    mfaani about 3 years
    for anyone thinking what the f prefix/suffix is for. I initially thought the f in printf / sprintf / scanf means file. But it just means format.