您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下C中printf、sprintf和fprintf的区别有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
printf:
printf函数用于在stdout(标准输出)控制台打印字符流数据。
语法:
int printf(const char* str, ...);
示例:
#include<stdio.h>
int main()
{
printf("hello geeksquiz");
return 0;
}输出:
hello geeksquiz
sprintf:
语法:
int sprintf(char *str, const char *string,...);
sprintf用于将格式化文本(字符串/字符流)打印到字符串缓冲区上。
示例:
#include<stdio.h>
int main()
{
char buffer[50];
int a = 10, b = 20, c;
c = a + b;
sprintf(buffer, "Sum of %d and %d is %d", a, b, c);
printf("%s", buffer);
return 0;
}输出:
Sum of 10 and 20 is 30
fprintf:
fprintf用于在文件中打印字符串内容,但不在stdout(标准输出)控制台上打印。
int fprintf(FILE *fptr, const char *str, ...);
示例:
#include<stdio.h>
int main()
{
int i, n=2;
char str[50];
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;
}输入: GeeksforGeeks
GeeksQuiz
输出: sample.txt file now having output as
0. GeeksforGeeks
1. GeeksQuiz以上是C中printf、sprintf和fprintf的区别有哪些的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。