Home > Backend Development > C#.Net Tutorial > What is the difference between printf, sprintf and fprintf in C language

What is the difference between printf, sprintf and fprintf in C language

青灯夜游
Release: 2019-01-25 17:02:01
Original
14155 people have browsed it

printf, sprintf and fprintf are all output statements in C language, and they all output formatted strings. So what are the differences between these three? In this article, we will learn about printf, sprintf and fprintf, and introduce the differences between them. I hope it will be helpful to everyone.

What is the difference between printf, sprintf and fprintf in C language

printf

The printf function is used to output text on the standard output device (stdout console) (string/char stream) or value.

Basic syntax

int printf(const char * format,...);
Copy after login

Description:

format provides the format of a text string that will be used on the output device using the formats %s, %d, %f, etc. specifier for output.

...Provide the parameter list that needs to be output.

Return type int returns the total number of characters output on the screen.

Example:

#include<stdio.h> 
int main() 
{ 
   printf("hello geeksquiz"); 
    printf("\n"); 
   int  a=2;
   printf("%d",a); 
   return 0; 
}
Copy after login

Output:

What is the difference between printf, sprintf and fprintf in C language

##sprintf

sprintf is used to send (copy) formatted text (string/character stream) to a string buffer.

Basic syntax

int sprintf(char * str,const char * format,...);
Copy after login

Description:

char * str : A character array in which the formatted text will be sent (copied).

formatProvides formatted text with the help of format specifiers.

...Provide the parameter list that needs to be output.

● The return type int returns the total number of copied (sent) characters to char * str.

Example:

#include <stdio.h>
int main()
{
    char str[100];
    int n;
    
    n=sprintf((char*)str,"我的名字是%s, I am %d years old.","Mike",23);
    
    printf("Text is: %s\n",str);
    printf("Total number of copied characters are: %d\n",n);
    return 0;
}
Copy after login

Description: sprintf stores the string in the specified char buffer, and then outputs it on the stdout console through printf.

Output:

What is the difference between printf, sprintf and fprintf in C language

fprintf

fprintf is used to output characters in a file String content, but not output on the stdout console.

Basic syntax:

int fprintf(FILE * fptr,const char * str,...);
Copy after login

Description:

fptr: This is a pointer to the FILE object that identifies the stream.

str: ​​This is a C string containing the text to be written to the stream stream.

Sample:

#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("无法打开文件"); 
        return 0; 
    } 
  
    for (i=0; i<n; i++) 
    { 
        puts("输入名称"); 
        gets(str); 
        fprintf(fptr,"%d.%s\n", i, str); 
    } 
    fclose(fptr); 
  
    return 0; 
}
Copy after login
Output:

What is the difference between printf, sprintf and fprintf in C language

In sample.txt:


What is the difference between printf, sprintf and fprintf in C language

Summary:

The difference between printf, sprintf and fprintf is that their output targets are different. printf outputs a data character stream on the stdout console; sprintf sends the data character stream to the specified char buffer; fprintf is used to output string content in a file.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between printf, sprintf and fprintf in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template