比较不同Between Similar Terms

Difference Between

Home / Technology / IT / Programming /Difference Between printf and fprintf

Difference Between printf and fprintf

January 3, 2018Posted byLithmee

Key Difference – printf vs fprintf

Afunctionis 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 asC 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. Thekey differencebetween print and fprintf is thatprintf 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”);

Difference Between printf and fprintf

Figure 01: printf()

If the user does not want to print a formatted string, it is possible to print the string as it is.

例如printf(“Hello World”);

Method toprint a formatted stringis 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 numbersis as follows. Refer bellow example.

int main(){

float area= 20.45;

printf(“Area is % 4.2f”, area);

return 0;

}

Printing charactersare as follows.

int main(){

char letter = ‘A’;

printf(“Letter is %c”, letter);

return 0;

}

Printing stringsis as follows.

int main(){

char word[6]= “hello”;

printf(“ Word is %s”, word);

return 0;

}

格式化字符串也可以有转义序列。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.

Toprint 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

#include

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 hereDifference 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)viaCommons Wikimedia

Related posts:

Difference Between Dictionary and Hashtable Difference Between Semaphore and Monitor Difference Between Definite Loop and Indefinite Loop Difference Between Assembly and DLL Difference Between Java5 and Java6

Filed Under:ProgrammingTagged With:Compare printf and fprintf,fprintf,fprintf Definition,fprintf Function,fprintf Syntax,printf,printf and fprintf Differences,printf and fprintf Similarities,printf Definition,printf Function,printf Syntax,printf vs fprintf

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Tense and Aspect

Difference Between Tense and Aspect

What is the Difference Between Surgical Steel and Stainless Steel

What is the Difference Between Surgical Steel and Stainless Steel

Difference Between Microsoft Windows Mobile and Google Android

Difference Between Microsoft Windows Mobile and Google Android

Difference Between LTE and WiMAX

Difference Between Mali-400MP GPU and Adreno 220 GPU

Latest Posts

  • What is the Difference Between Aerogel and Xerogel
  • What is the Difference Between Bacitracin and Neosporin
  • What is the Difference Between Genetic Engineering and Genome Editing
  • What is the Difference Between Urinary Retention and Urinary Incontinence
  • What is the Difference Between FASTA and FASTQ
  • What is the Difference Between Textbook and Reference Book
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.