0
点赞
收藏
分享

微信扫一扫

IO-库

Mhhao 2022-07-18 阅读 52

@[TOC]

#include<stdio.h>

sscanf() 格式化写入,但不是从键盘输入

int sscanf(const char *str, const char *format, ...)

​const char *str​​: 指针->数据来源

​const char *format, ...​​: 格式化%,接收数据的参数列表

函数会返回接收了几个参数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int day, year;
char weekday[20], month[20], dtm[100];
int len=0;

strcpy( dtm, "Saturday March 25 1989" );
len=sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );

printf("%s %d, %d = %s\n", month, day, year, weekday );
printf("%d\n",len);

return(0);
}

输出

March 25, 1989 = Saturday
4
请按任意键继续. . .

sprintf–格式化 数字变为字符串化

这个函数与

int sprintf(char *str, const char *format, ...)

Eg:

sprintf(str, "Pi 的值 = %f", M_PI);

​str​​是一个字符串的名字

​"Pi 的值 = %f"​​格式化`写入

​M_PI​​写入的参数

该函数成功写入会返回写入的长度,不包括字符串追加在字符串末尾的空字符

失败就返回负数

#include <stdio.h>
#include <math.h>

int main()
{
char str[80];
int len=0;

len=sprintf(str, "the value of PI is %.5f", M_PI);

puts(str);
printf("%d\n",len);

return(0);
}

IO-库_C

输出

the value of PI is 3.14159
26
请按任意键继续. . .

举报

相关推荐

0 条评论