Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between fprintf, printf and sprintf?

Tags:

c

io

stream

printf

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".

like image 916
Vishwanath Dalvi Avatar asked Jan 07 '11 15:01

Vishwanath Dalvi


People also ask

What is the difference between printf () and fprintf () in the C language?

The C language already provides them. The difference between printf and fprintf is that printf is used to print a formatted string to a standard output which is most of the time a computer screen and fprintf is used to print a formatted string to a specific file. printf and fprintf can be used according to the task.

Is sprintf faster than printf?

12, printf() takes 42682 cycles and sprintf() takes 38955 cycles. This is with -msmart-io=2 enabled. The speed difference is likely due to the time waiting for the I/O.

What is difference between printf () and sprintf () in PHP?

The sprintf() function is similar to the printf() function, but the only difference between both of them is that sprint() saves the output into a string instead of displaying the formatted message on browser like printf() function.

What is the use of sprintf in Matlab?

str = sprintf( literalText ) translates escape-character sequences in literalText , such as \n and \t . It returns all other characters unaltered. If literalText contain a formatting operator (such as %f ), then str discards it and all characters after.


7 Answers

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.

like image 157
John Bode Avatar answered Oct 01 '22 18:10

John Bode


printf outputs to the standard output stream (stdout)

fprintf goes to a file handle (FILE*)

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

like image 23
Moo-Juice Avatar answered Oct 01 '22 17:10

Moo-Juice


  • 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.

like image 44
Rubal Avatar answered Oct 01 '22 17:10

Rubal


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

like image 27
VGE Avatar answered Oct 01 '22 19:10

VGE


You can also do very useful things with vsnprintf() function:

$ cat test.cc
#include <exception>
#include <stdarg.h>
#include <stdio.h>

struct exception_fmt : std::exception
{
    exception_fmt(char const* fmt, ...) __attribute__ ((format(printf,2,3)));
    char const* what() const throw() { return msg_; }
    char msg_[0x800];
};

exception_fmt::exception_fmt(char const* fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(msg_, sizeof msg_, fmt, ap);
    va_end(ap);
}

int main(int ac, char** av)
{
    throw exception_fmt("%s: bad number of arguments %d", *av, ac);
}

$ g++ -Wall -o test test.cc

$ ./test
terminate called after throwing an instance of 'exception_fmt'
  what():  ./test: bad number of arguments 1
Aborted (core dumped)
like image 41
Maxim Egorushkin Avatar answered Oct 01 '22 18:10

Maxim Egorushkin


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
like image 31
Amit Vasava Avatar answered Oct 01 '22 17:10

Amit Vasava


sprintf: Writes formatted data to a character string in memory instead of stdout

Syntax of sprintf is:

#include <stdio.h>
int sprintf (char *string, const char *format
[,item [,item]…]);

Here,

String refers to the pointer to a buffer in memory where the data is to be written.

Format refers to pointer to a character string defining the format.

Each item is a variable or expression specifying the data to write.

The value returned by sprintf is greater than or equal to zero if the operation is successful or in other words the number of characters written, not counting the terminating null character is returned and returns a value less than zero if an error occurred.

printf: Prints to stdout

Syntax for printf is:

printf format [argument]…

The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device.

like image 43
Fahad Ali Avatar answered Oct 01 '22 18:10

Fahad Ali