知识点1【格式化字符串操作函数】

格式化字符串操作函数_字符串

1、组包 sprintf

代码解读
int sprintf(char *str, const char *format, ...);
功能:将数据 以format的格式 组成字符串 写入str指向的空间中
  • 1.
  • 2.

案例1:

格式化字符串操作函数_字符串_02

案例2:将数值 转成 字符串

格式化字符串操作函数_const_03

案例3:将字符串 转成 数值 atoi atol atof

比如:字符串"123"转成十进制123

比如:字符串"123abc456"转成十进制123

代码解读
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
  • 1.
  • 2.
  • 3.
  • 4.

格式化字符串操作函数_格式化字符串_04

2、解包 sscanf

代码解读
int sscanf(const char *buf,const char *format, …);
功能:将buf指向的字符串 按照format格式 解出 独立数据
  • 1.
  • 2.

案例1:将2024年3月9日 解出 独立数据

代码解读
void test03()
{
    int year = 0;
    int month = 0;
    int day = 0;

    sscanf("2024年3月9日", "%d年%d月%d日", &year, &month, &day);

    printf("%d %d %d\n", year, month, day);//2024 3 9
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

3、sscanf 高级用法

1、跳过数据:%* s 或%* d

代码解读
sscanf("1234 5678", "%*d %s", buf);//"5678"
  • 1.

2、读指定宽度的数据:%[width]s

%2d 只能提取2个数值

%3s 只能提取3个字符

代码解读
sscanf("12345678", "%4s", buf);//buf="1234"

int data=0;
char buf[32]="";
sscanf("12345678", "%*2d%2d%*c%2s", &data,buf);//data=34  buf="67"
sscanf("12345678", "%*2s%2d%*c%2s", &data,buf);//data=34  buf="67"
sscanf("12345678", "%*c%*c%2d%*c%2s", &data,buf);//data=34  buf="67"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3、%[a-z] 表示匹配 a 到 z 中任意字符(尽可能多的匹配)

%[]提取的结果是字符串。

代码解读
char buf[20];
sscanf("agcd32DajfDdFF","%[a-z]",buf);//buf="agcd"
  • 1.
  • 2.

4、%[aBc] 匹配 a、B、c 中一员,贪婪性

代码解读
char buf[20];
sscanf("aBacb","%[aBc]",buf);//buf="aBac"
  • 1.
  • 2.

5、% [ ^ aFc ]匹配非 a Fc 的任意字符,贪婪性

代码解读
char buf[20];
sscanf("bcabc","%[^aBc]",buf);//buf="b"
  • 1.
  • 2.

6、%[ ^a-z ]表示读取除 a-z 以外的所有字符

代码解读
char buf[20];
sscanf("123abc","%[^a-z]",buf);//buf="123"
  • 1.
  • 2.

4、综合案例

代码解读
void test05()
{
    char buf[]="[01:19.22][02:39.22][03:49.22]我想大声宣布对你依依不舍";
    char *song_p = buf;
    char *time_p = buf;
    int i=0;

    //找到歌词的起始位置
    while(*song_p == '[')
     song_p+=10;

    while(*time_p =='[')//说明是时间
    {
        int m = 0,s=0;
        sscanf(time_p,"[%d:%d", &m,&s);
        printf("%d秒唱:%s\n",m*60+s, song_p);
        time_p+=10;
    }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

格式化字符串操作函数_const_05

知识点2【const和指针】

代码解读
int data = 10;
//const 修饰* 表示*p为只读   p可读可写(指向其他地方)
const int * p = &data;
//*p = 1000;//error

int num = 20;
p=#
printf("*p =%d\n", *p);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
代码解读
int data = 10;
//const 修饰p 表示p为只读(不能更改指向)   *p可读可写
int * const p = &data;
*p = 1000;
printf("*p =%d\n", *p);

int num = 20;
//p=#//error
printf("*p =%d\n", *p);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
代码解读
int data = 10;
//const 修饰p 表示p为只读(不能更改指向)   *p只读(不能通过*p修改空间内容)
const int * const p = &data;
//*p = 1000;//error
printf("*p =%d\n", *p);

int num = 20;
//p=#//error
printf("*p =%d\n", *p);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.