Difference Between printf and fprintf
Table of Contents
Key Difference – printf vs fprintf
A function is a set of instructions to perform a specific task. It is not possible to write all statements in the same program. Therefore, the program is divided into several functions. Functions provide code reusability. In programming language such as C language, main() is a function. It indicates the starting point of the execution. There are built-in functions and user-defined functions. The programmer creates user-defined functions. The language provides built-in functions. The programmer can use them without implementing from the beginning. Two main built-in functions in C language are printf() and fprintf(). This article discusses the difference between these two functions. The key difference between print and fprintf is that printf is a C function used to print a formatted string to a standard output stream which is the computer screen, while fprintf is a C function to print a formatted string to a file.
CONTENTS
1. Overview and Key Difference
2. What is printf
3. What is fprintf
4. Similarities Between printf and fprintf
5. Side by Side Comparison – printf vs fprintf in Tabular Form
6. Summary
What is printf?
“printf” function is used to give an output in a formatted way to a display device such as computer screen. The syntax of printf function is as follows.
printf(“formatted string”, “list of variables”);
Figure 01: printf()
If the user does not want to print a formatted string, it is possible to print the string as it is.
e.g. printf(“Hello World”);
Method to print a formatted string is as follows. Refer bellow example. “a” and “b” are integers, so they are specified with %d.
int main(){
int a=10,b=20;
printf(“Value of a is %d and value of b is %d\n”, a,b);
return 0;
}
Printing floating point numbers is as follows. Refer bellow example.
int main(){
float area= 20.45;
printf(“Area is % 4.2f”, area);
return 0;
}
Printing characters are as follows.
int main(){
char letter = ‘A’;
printf(“Letter is %c”, letter);
return 0;
}
Printing strings is as follows.
int main(){
char word[6]= “hello”;
printf(“ Word is %s”, word);
return 0;
}
Formatted string can also have escape sequences. They start with a backslash (“\”). Some of them are \n and \t.
int main(){
int a=10,b=20;
printf(“value of a is %d \n value of b is %d\n”, a,b);
return 0;
}
This will print “a” and “b” values in separate lines.
printf(“value of a is %d \t value of b is %d\n”, a,b); will give a space or a tab between value of a and value of b.
To print double quotes, the programmer can use as follows.
printf(“Learning \“C \” programming”);
What is fprintf?
The fprinf function is used to output a formatted string to a file. The syntax for fprintf is as follows;
fprintf(file pointer, “format specifier”, “list of variables”);
Refer the below code to understand the functionality of fprintf ().
#include <stdio.h>
#include <conio.h>
int main(){
FILE *ptr;
char name[5]= “Ann”;
int id= 3;
ptr = fopen(“file1.txt”, “w”);
if (ptr ==NULL){
printf(“Unable to open the file\n”);
}
else{
fprintf(ptr,”%s , %d”, name,id);
printf(“Data is written successfully to the file”);
fclose(ptr);
}
getch();
return 0;
}
“ptr” is a pointer to a file. The file is opened in write mode. If it is not opened, it will give unable to open the file error. If it opens successfully, the formatted string is printed to the file. File pointer, formatted string and the variable list is passed to the fprintf function. Finally, the file is closed using fclose(). To append data to the file, the statement can be changed as follows.
ptr = fopen(“file1.txt”, “a”);
What is the Similarity Between printf and fprintf?
- Both are functions provided by the C language.
What is the Difference Between printf and fprintf?
printf vs fprintf | |
printf is a C function to print a formatted string to the standard output stream which is the computer screen. | fprintf is a C function to print a formatted string to a file. |
Syntax | |
Formatted string and list of parameters are passed to printf function. e.g. printf(“format”, args); | File pointer, formatted string and list of parameters are passed to the fprintf function. e.g. fprintf(File *ptr, “format”, args ); |
Summary – printf vs fprintf
“printf” and “fprintf” are functions in C. Programmer does not need to implement these functions from the beginning. 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.
Download the PDF Version of printf vs fprintf
You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between printf and fprintf
Reference:
1.tutorialspoint.com. “Computer Programming Functions.” Available here
2.LearningLad. YouTube, YouTube, 6 May 2013. Available here
3.LearningLad. YouTube, YouTube, 23 Apr. 2013. Available here
Image Courtesy:
1.’Printf’By I, Surachit, (CC BY-SA 3.0) via Commons Wikimedia
ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau2680aKlrZ5dlruledWsZJ%2Boop67tbKO