当前位置:网站首页>Explanation of sprintf function in C language
Explanation of sprintf function in C language
2022-06-28 13:39:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
srpintf() The function is very powerful : More efficient than some string manipulation functions ; And more flexible ; You can output the desired result to the specified string , It can also be used as a buffer , and printf Can only be output to the command line ~
The header file :stdio.h
The functionality : Formatted string , Write the formatted data to the string .
The function prototype :int sprintf(char *buffer, const char *format, [argument]…)
Parameters :
(1)buffer: yes char Pointer to type , Pointer to the written string ;
(2)format: Formatted string , That is, the format you want in the program ;
(3)argument: Optional parameters , Can be any type of data ;
Function return value :buffer The length of the string pointed to ;
use :
(1) Format numeric strings : At this point sprintf and printf In the same way , It's just that the printing location is different , The former is printed to buffer character string , The latter is printed to standard output , therefore sprintf It can also be used to convert an integer to a string , Than itoa Efficient and so simple ~ such as :sprintf(buffer, “%d”, 123456); After execution buffer That is to point to the string “123456”~
(2) Connecting characters :
Here are two examples to illustrate this problem :
(a) Connect with ’\0’ Ending string :
#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;
}
The result is 12345678.
(b) There is no... At the end of the connection ’\0’ Character array or string buffer for :
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;
}
The result is 12345678.
(c) If we want to dynamically get the length of the character buffer to be processed , Then put the top sprintf Change it to :sprintf(buffer, “%.*s%.*s”, sizeof(a), a, sizeof(b), b); that will do ~
(3) utilize sprintf Return value in
because sprintf The return value of the function is output+pos The length of the string pointed to , So for pos Come on , Equivalent to one execution pos+=sizeof(output+pos), If this statement is placed in a loop , Then execute... For the second time sprintf when output+pos It then points to the end of the current buffer ( Notice that it's not output At the end of ! Otherwise, illegal memory will be read !), In this way, a string with certain rules can be generated ~ Write an example :
#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';// Put the last character '-' Convert to '\n'
printf(buf);
return 0;
}
pos Each time 2, Because every time buf The string must be followed by two characters j and ‘-’. give the result as follows
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150700.html Link to the original text :https://javaforall.cn
边栏推荐
- ThreadLocal的简单理解
- En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
- PostgreSQL surpasses MySQL
- Yii2 connects to websocket service to realize that the server actively pushes messages to the client
- Centos7: switch MySQL users and log in to MySQL
- PCB懂王,你是吗?我不是
- Idea global search shortcut settings
- Mysql database literacy, do you really know what a database is
- Professional English calendar questions
- [codec] write H264 decoder (1) from scratch
猜你喜欢
中国数据库技术大会(DTCC)特邀科蓝SUNDB数据库专家精彩分享
Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
Setup and upload of arduino-esp32 flash file plug-in program
StackOverflow 2022数据库年度调查
From PDB source code to frame frame object
Kubernetes 深入理解Kubernetes(二) 声明组织对象
众昂矿业着眼氟化工产业,布局新能源产业链
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
随机推荐
Prediction of red wine quality by decision tree
Centos7:切换mysql用户并登录mysql
yii2连接websocket服务实现服务端主动推送消息给客户端
股票网上开户及开户流程怎样?手机开户是安全么?
Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
Visual design tutorial of word cloud
恒生电子:金融分布式数据库LightDB通过中国信通院多项测评
Hisilicon 35xx realizes gt911 touch screen function "suggestions collection"
How vscade sets auto save code
Flutter series part: detailed explanation of GridView layout commonly used in flutter
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
Notes on the use of official jeecg components (under update...)
In the past four years, the number of users exceeded 100 million, and sun Ge led the wave field to a new high
Jeecg 官方组件的使用笔记(更新中...)
MySQL多表联合查询
排序
Fs7022 scheme series fs4059a dual two lithium battery series charging IC and protection IC
(original) [Maui] realize "floating action button" step by step
Yii2 connects to websocket service to realize that the server actively pushes messages to the client
再談exception——异常拋出時會發生什麼?