当前位置:网站首页>C语言 sprintf函数使用详解
C语言 sprintf函数使用详解
2022-06-28 12:04:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
srpintf()函数的功能非常强大:效率比一些字符串操作函数要高;而且更具灵活性;可以将想要的结果输出到指定的字符串中,也可作为缓冲区,而printf只能输出到命令行上~
头文件:stdio.h
函数功能:格式化字符串,将格式化的数据写入字符串中。
函数原型:int sprintf(char *buffer, const char *format, [argument]…)
参数:
(1)buffer:是char类型的指针,指向写入的字符串指针;
(2)format:格式化字符串,即在程序中想要的格式;
(3)argument:可选参数,可以为任意类型的数据;
函数返回值:buffer指向的字符串的长度;
用处:
(1)格式化数字字符串:在这点上sprintf和printf的用法一样,只是打印到的位置不同而已,前者打印给buffer字符串,后者打印给标准输出,所以sprintf也可以用来将整型转化为字符串,比itoa效率高且如此地简便~比如:sprintf(buffer, “%d”, 123456);执行后buffer即指向字符串“123456”~
(2)连接字符:
下面通过两个例子来说明这个问题:
(a)连接以’\0’结束的字符串:
#include<stdio.h>
int main()
{
char buffer[10];
char *a = "1234";
char *b = "5678";
sprintf(buffer, "%s%s", a, b);
printf("%s\n", buffer);
return 0;
} 结果是12345678。
(b)连接结尾没有’\0’的字符数组或字符串缓冲区:
include<stdio.h>
int main()
{
char a[] = {'1', '2', '3', '4'};
char b[] = {'5', '6', '7', '8'};
char buffer[10];
sprintf(buffer, "%.4s%.4s", a, b);
printf("%s\n", buffer);
return 0;
}结果是12345678.
(c)如果我们想动态获取要处理的字符缓冲区长度,则将上面sprintf改为:sprintf(buffer, “%.*s%.*s”, sizeof(a), a, sizeof(b), b);即可~
(3)利用sprintf中的返回值
因为sprintf函数的返回值为output+pos所指向字符串的长度,所以对于pos来说,相当于执行了一次pos+=sizeof(output+pos),如果这条语句放在一个循环里,则第二次执行sprintf时output+pos随即指向了当前缓冲区的末尾(注意不是output的末尾!否则会读取非法内存!),这样就可以生成一个具有一定规则的字符串了~写个例子:
#include<stdio.h>
int main()
{
char buf[100];
int pos = 0;
for(int j = 0; j < 10; j++)
pos += sprintf(buf+pos, "%d-", j);
buf[pos-1] = '\n';//将最后一个字符'-'转换为'\n'
printf(buf);
return 0;
}pos每次增加2,因为每次buf字符串后面都要加上两个字符j和‘-’。结果如下
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150700.html原文链接:https://javaforall.cn
边栏推荐
- Still using simpledateformat for time formatting? Be careful that the project collapses!
- JS foundation 10
- 女子高考落榜读专科逆袭买千万别墅,考得不好真的没关系
- What method is required for word, PDF and txt files to realize full-text content retrieval?
- Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
- Zero basic C language (I)
- Many benefits of SEO optimization are directly related to traffic
- Using soapUI to obtain freemaker's FTL file template
- In less than an hour, apple destroyed 15 startups
- Prefix and (one dimension)
猜你喜欢

JS foundation 8

Prefix and (2D)

Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly

RemoteViews的作用及原理

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

JNI confusion of Android Application Security

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...

Leetcode 705. 设计哈希集合

【C语言】二叉树的实现及三种遍历
![[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
随机推荐
Source code analysis of ArrayList
设置Canvas的 overrideSorting不生效
【Unity编辑器扩展实践】、通过代码查找所有预制
Redis principle - List
Map sorting tool class
Usage and principle of precomputedtextcompat
多维度监控:智能监控的数据基础
JS foundation 8
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
PyQt5可视化开发
2018 joint examination of nine provinces & Merging of line segment trees
期货开户有门槛吗,如何网上安全的开通期货账户
【C语言】NextDay问题
开源项目维权成功案例: spug 开源运维平台成功维权
内部振荡器、无源晶振、有源晶振有什么区别?
Difference (one dimension)
Custom title bar view
2022年理财产品的常见模式有哪些?
Graphics view framework for QT learning (to realize startup animation)
Sha256 encryption tool class