What is the Difference Between printf and fprintf?

🆚 Go to Comparative Table 🆚

The main difference between printf and fprintf lies in the destination of their output. Here's a summary of their differences:

  • printf: This function is used to print character data on the standard output stream (stdout), which is typically the console or monitor. The syntax for printf is: printf(const char* format, ...);.
  • fprintf: This function is used to print character data on a named output stream, such as a file. The syntax for fprintf is: fprintf(FILE *stream, const char *format, ...);. The FILE *stream parameter is a file pointer that indicates the file where the formatted data will be saved.

In summary, printf is used to display output on the console, while fprintf is used to write output to a specified file or output stream.

Comparative Table: printf vs fprintf

The main difference between printf and fprintf is that printf is used to print a formatted string to the standard output stream (stdout), while fprintf is used to print a formatted string to a file. Here is a table summarizing their differences:

Function Output Destination Syntax Description
printf Standard output stream (stdout) printf(const char* format, ...) Prints formatted string to the console
fprintf File fprintf(FILE *fptr, const char *format, ...) Prints formatted string to a file specified by the file pointer fptr

Typically, printf is used when you want to produce output to the console for a user, while fprintf is used to produce output to a file.