What is the difference between printf https://www.skillvertex.com/blog Wed, 06 Mar 2024 11:39:37 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png What is the difference between printf https://www.skillvertex.com/blog 32 32 What Is The Difference Between Printf, Sprintf, And Fprintf? https://www.skillvertex.com/blog/what-is-the-difference-between-printf-sprintf-and-fprintf/ https://www.skillvertex.com/blog/what-is-the-difference-between-printf-sprintf-and-fprintf/#respond Wed, 06 Mar 2024 11:39:37 +0000 https://www.skillvertex.com/blog/?p=3647 Read more]]>

Table of Contents

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

printf(const char* str, ...); 

Example



// simple print on stdout
#include <stdio.h>
int main()
{
    printf("hello skillvertex");
    return 0;
}

Output

Hello Skillvertex

Sprintf Function:

The sprintf function is similar to printf, 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.

Syntax

sprintf(char *str, const char *string,...); 

Example

#include <stdio.h>
int main()
{
    int i, n = 2;
    char str[50];
 
    // open file sample.txt in write mode
    FILE* fptr = fopen("sample.txt", "w");
    if (fptr == NULL) {
        printf("Could not open file");
        return 0;
    }
 
    for (i = 0; i < n; i++) {
        puts("Enter a name");
        gets(str);
        fprintf(fptr, "%d.%s\n", i, str);
    }
    fclose(fptr);
 
    return 0;
}

Output

Sum of 10 and 20 is 30

fprintf Function:

The fprintf 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.

fprintf(FILE *fptr, const char *str, ...);

Example

#include <stdio.h>
int main()
{
    int i, n = 2;
    char str[50];
 
    // open file sample.txt in write mode
    FILE* fptr = fopen("sample.txt", "w");
    if (fptr == NULL) {
        printf("Could not open file");
        return 0;
    }
 
    for (i = 0; i < n; i++) {
        puts("Enter a name");
        gets(str);
        fprintf(fptr, "%d.%s\n", i, str);
    }
    fclose(fptr);
 
    return 0;
}

Input

Enter a name Skillvertex
Enter a name SkillQuiz

Output

0. Skillvertex
1. SkillQuiz

FAQ- What is the difference between printf, sprintf and fprintf?

Q1. What is the difference between sprintf and fprintf?

Ans.fprintf vs. printf:
fprintf:
Writes formatted output to a specified file or stream.
Example: fprintf(filePointer, "The value is %d", value);
printf:
Writes formatted output to the standard output console (stdout).
Example: printf("The value is %d", value);
In essence, fprintf deals with files or streams, while printf outputs to the console.

Q2. What is fprintf () used for?

Ans.fprintf:
Writes information to a specified file or stream.
Useful for user interaction and screen or file output.
printf:
Stands for “formatted print.”
Prints data to the console with a specified format, enhancing readability.

Q3. What is the difference between printf and print format?

Ans. printf():
Prints a formatted string to the standard output console (stdout).
Example: printf("Formatted data: %d", formattedValue);
sprintf():
Returns a formatted string that can be assigned to a variable.
Example: char buffer[20]; sprintf(buffer, "Formatted data: %d", formattedValue);

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/what-is-the-difference-between-printf-sprintf-and-fprintf/feed/ 0