{"id":3647,"date":"2024-03-06T11:39:37","date_gmt":"2024-03-06T11:39:37","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3647"},"modified":"2024-03-06T11:39:37","modified_gmt":"2024-03-06T11:39:37","slug":"what-is-the-difference-between-printf-sprintf-and-fprintf","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/what-is-the-difference-between-printf-sprintf-and-fprintf\/","title":{"rendered":"What Is The Difference Between Printf, Sprintf, And Fprintf?"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-the-difference-between-printf-sprintf-and-fprintf\">What is the difference between printf, sprintf and fprintf?<\/a><\/li><li ><a href=\"#printf-function\">Printf function<\/a><\/li><li ><a href=\"#sprintf-function\">Sprintf Function:<\/a><\/li><li ><a href=\"#faq-what-is-the-difference-between-printf-sprintf-and-fprintf\">FAQ- What is the difference between printf, sprintf and fprintf?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-difference-between-printf-sprintf-and-fprintf\">What is the difference between printf, sprintf and fprintf?<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"printf-function\">Printf function<\/h2>\n\n\n\n<p>The <code>printf<\/code> function is primarily used to display a character stream of data on the console (stdout) in programming<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(const char* str, ...); \n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ simple print on stdout\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"hello skillvertex\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sprintf-function\"><strong>Sprintf Function:<\/strong><\/h2>\n\n\n\n<p>The <code>sprintf<\/code> function is similar to <code>printf<\/code>, but instead of printing the character stream on the console, it stores the formatted output into a specified character buffer. This allows you to capture and manipulate the formatted string within your program rather than displaying it immediately on the console.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sprintf(char *str, const char *string,...); \n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint main()\n{\n    int i, n = 2;\n    char str&#91;50];\n \n    \/\/ open file sample.txt in write mode\n    FILE* fptr = fopen(\"sample.txt\", \"w\");\n    if (fptr == NULL) {\n        printf(\"Could not open file\");\n        return 0;\n    }\n \n    for (i = 0; i &lt; n; i++) {\n        puts(\"Enter a name\");\n        gets(str);\n        fprintf(fptr, \"%d.%s\\n\", i, str);\n    }\n    fclose(fptr);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum of 10 and 20 is 30\n<\/code><\/pre>\n\n\n\n<p><strong>fprintf Function:<\/strong><\/p>\n\n\n\n<p>The <code>fprintf<\/code> function is employed to print the content of a string or other formatted data into a file, rather than displaying it on the standard output console (stdout). This function allows you to direct the output to a specified file, providing a way to store formatted information in a file within your program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fprintf(FILE *fptr, const char *str, ...);\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint main()\n{\n    int i, n = 2;\n    char str&#91;50];\n \n    \/\/ open file sample.txt in write mode\n    FILE* fptr = fopen(\"sample.txt\", \"w\");\n    if (fptr == NULL) {\n        printf(\"Could not open file\");\n        return 0;\n    }\n \n    for (i = 0; i &lt; n; i++) {\n        puts(\"Enter a name\");\n        gets(str);\n        fprintf(fptr, \"%d.%s\\n\", i, str);\n    }\n    fclose(fptr);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Input<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a name Skillvertex\nEnter a name SkillQuiz<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0. Skillvertex\n1. SkillQuiz<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-what-is-the-difference-between-printf-sprintf-and-fprintf\">FAQ- What is the difference between printf, sprintf and fprintf?<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700559805579\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the difference between sprintf and fprintf?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>fprintf vs. printf:<\/strong><br \/><strong>fprintf:<\/strong><br \/>Writes formatted output to a specified file or stream.<br \/>Example: <code>fprintf(filePointer, \"The value is %d\", value);<\/code><br \/><strong>printf:<\/strong><br \/>Writes formatted output to the standard output console (stdout).<br \/>Example: <code>printf(\"The value is %d\", value);<\/code><br \/>In essence, <code>fprintf<\/code> deals with files or streams, while <code>printf<\/code> outputs to the console.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700559815024\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is fprintf () used for?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>fprintf:<\/strong><br \/>Writes information to a specified file or stream.<br \/>Useful for user interaction and screen or file output.<br \/><strong>printf:<\/strong><br \/>Stands for &#8220;formatted print.&#8221;<br \/>Prints data to the console with a specified format, enhancing readability.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700559822315\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the difference between printf and print format?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>printf():<\/strong><br \/>Prints a formatted string to the standard output console (stdout).<br \/>Example: <code>printf(\"Formatted data: %d\", formattedValue);<\/code><br \/><strong>sprintf():<\/strong><br \/>Returns a formatted string that can be assigned to a variable.<br \/>Example: <code>char buffer[20]; sprintf(buffer, \"Formatted data: %d\", formattedValue);<\/code><\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>What is the difference between printf, sprintf and fprintf? Printf function The printf function is primarily used to display a character stream of data on the console (stdout) in programming Syntax Example Output Sprintf Function: The sprintf function is similar to printf, but instead of printing the character stream on the console, it stores the &#8230; <a title=\"What Is The Difference Between Printf, Sprintf, And Fprintf?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/what-is-the-difference-between-printf-sprintf-and-fprintf\/\" aria-label=\"More on What Is The Difference Between Printf, Sprintf, And Fprintf?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3651,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[599],"class_list":["post-3647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-what-is-the-difference-between-printf","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3647"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3647\/revisions"}],"predecessor-version":[{"id":7953,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3647\/revisions\/7953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3651"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}