What is the difference between printf, sprintf and fprintf? Last Updated : 03 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The printf() function is used as a standard method for output operations and C also provides some different versions of this function such as sprintf() and fprintf(). These functions are also used for output operations but in different contexts.The below table lists the primary differences between the printf, sprintf and fprintf in C:FunctionPurposeOutput LocationSyntaxUse Caseprintf()Prints formatted output.Standard output (console).printf(format, arguments);Use when you want to display formatted text to the console, such as for debugging.sprintf()Writes formatted output to a string.A character array (string).sprintf(buffer, format, arguments);Use when you need to store formatted data in a string, such as for custom error messages.fprintf()Prints formatted output to a specific stream.A file or stream specified by FILE*fprintf(stream, format, arguments);Use when you need to write formatted data to a file or another stream, such as for logging.Let's see each of them in detail one by one.printf()The printf function is used to print formatted output to the standard output, typically the console or terminal.Example C #include <stdio.h> int main() { int n = 42; // Print integer and floating-point // number to the console printf("Number is %d", n); return 0; } OutputNumber is 42Explanation: The above program demonstrates how to print to the console using the printf function in C by passing it inside a formatted string.sprintfThe sprintf function is similar to printf, but instead of printing to the console, it stores the formatted string in a character array (string). This is useful when you need to generate a string dynamically for further processing.Example C #include <stdio.h> int main() { int n = 42; char res[50]; // Store formatted output in a string // using sprintf sprintf(res, "Number is %d.", n); // Print the string stored in result printf("%s", res); return 0; } OutputBook is 42. Explanation: The sprintf function is used to store the formatted string in the res array, which is then printed using printf.fprintfThe fprintf function is used to print formatted output to a file or any other stream, such as a printer or a log file. It works similarly to printf, but the output is directed to a file specified by the FILE pointer.Example C #include <stdio.h> int main() { int n = 42; FILE *file = fopen("file.txt", "w"); if (file != NULL) { // Write formatted output to a file fprintf(file, "Number is %d.\n", n); fclose(file); printf("Data written to output.txt\n"); } else { printf("Error opening file.\n"); } return 0; } OutputData written to output.txt Explanation: The fprintf function is used to write the formatted string to the file output.txt. If the file is successfully opened, the formatted output is written, the file is closed, and the message "Data written to output.txt" is printed to the console. Comment More infoAdvertise with us K kartik Improve Article Tags : C Language c-input-output C-Input and Output Quiz Similar Reads Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.Need of File Han 13 min read C fopen() Function In C, the fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further operations on the file, such as reading from or writing to it. If the file exists then the fopen() function opens the particular file else a new file 5 min read EOF, getc() and feof() in C In this article, we will discuss the EOF, getc() function and feof() function in C.What is EOF?In C, EOF is a constant macro defined in the <stdlib.h> header file that is used to denote the end of the file in C file handling. It is used by various file reading functions such as fread(), gets() 3 min read fgets() in C In C, fgets() is a built-in function defined in <stdio.h> header file. It reads the given number of characters of a line from the input stream and stores it into the specified string. This function stops reading the characters when it encounters a newline character, has read the given number o 3 min read fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt 1 min read scanf() and fscanf() in C In C language, scanf() function is used to read formatted input from stdin. It returns the whole number of characters written in it otherwise, returns a negative value.Syntax:int scanf(const char *characters_set)Time Complexity: O(n)Auxiliary Space: O(n) where n is the length of input.Many of us kno 4 min read C fread() Function The C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by f 4 min read fseek() vs rewind() in C In C, the functions fseek() and rewind() helps in manipulating the position of the pointer in the opened file but serve different purposes and have distinct working. The below table lists the major differences between fseek() and rewind() in C:Featurefseek()rewind()FunctionalityMoves the file pointe 2 min read What is return type of getchar(), fgetc() and getc() ? In C, getchar(), fgetc(), and getc() all are the functions used for reading characters from input buffer. This buffer is standard input buffer for getchar() and can be any specified file for getc() and fgetc(). In this article, we will learn about the return type of these functions and why it matter 3 min read Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr 3 min read Like