diff --git a/day12/d3.c b/day12/d3.c new file mode 100644 index 0000000..a56fe0d --- /dev/null +++ b/day12/d3.c @@ -0,0 +1,15 @@ +// 格式化输出字符串 sprintf(char *buf, const char *format, ...) +// 函数返回输出到 buf 字符串,并返回 buf 的字符串长度 +#include +#include +#include + +int main() +{ + char buf[20] = ""; + // ret 返回字符串 长度 + // %0nd : n为输出长度,不足n位的前面位置补0 + int ret = sprintf(buf, "%d 年 %02d 月 %d 日", 2023, 7, 18); + printf("ret = %d,%s\n", ret, buf); + return 0; +} \ No newline at end of file diff --git a/day12/d3_2.c b/day12/d3_2.c new file mode 100644 index 0000000..6b0d30d --- /dev/null +++ b/day12/d3_2.c @@ -0,0 +1,12 @@ +// sprintf() 实现字符串与字符串拼接,字符串与数字拼接,数字与数字拼接 +#include +#include + +int main() +{ + char buf1[20] = "hello"; + char buf2[20] = "world"; + sprintf(buf1, "%s %s", buf1, buf2); + printf("%s", buf1); + return 0; +} \ No newline at end of file diff --git a/day12/d4.c b/day12/d4.c new file mode 100644 index 0000000..db092f1 --- /dev/null +++ b/day12/d4.c @@ -0,0 +1,18 @@ +// 从字符串获取 sscanf(const char *buf, const char *format, ...); +// 从 buf 字符串按 format 格式提取数据并赋值给后面的变量地址 +#include +#include + +int main() +{ + char *content = "disen,17791692095,610039018"; + char name[10] = ""; // 换行符占最后一位 + long phone; + char qq[10] = ""; + + sscanf(content, "%5s,%ld,%9s", name, &phone, qq); + // sscanf(content, "%5s,%ld,%9s", name, &phone, qq); + printf("name: %s\nphone: %ld\nqq: %s\n", name, phone, qq); + + return 0; +} \ No newline at end of file diff --git a/day12/d5.c b/day12/d5.c new file mode 100644 index 0000000..bfa8844 --- /dev/null +++ b/day12/d5.c @@ -0,0 +1,18 @@ +// sscanf() 高级用法: 跳过内容 +#include +#include + +int main() +{ + char *content = "disen,17791692095,610039018"; + char first = 0; + char phone[12] = ""; + char qq[12] = ""; + sscanf(content, "%c", &first); + printf("%c\n", first); + // 跳过前6个字符,然后读取11个字符,放入 phone 中 + sscanf(content, "%*6s%11s", phone); + printf("%s\n", phone); + sscanf(content, "%*18s%11s", qq); + printf("%s\n", qq); +} \ No newline at end of file diff --git a/day12/d5_2.c b/day12/d5_2.c new file mode 100644 index 0000000..cbc9562 --- /dev/null +++ b/day12/d5_2.c @@ -0,0 +1,16 @@ +// sscanf() 高级用法: 跳过空格 +// 遇到空格则跳过 +#include +#include + +int main() +{ + char *content = "disen 17791692095,610039018"; + char phone[12] = ""; + // *s 表示跳过空格, 11s 表示读取11个字符 + sscanf(content, "%*s%11s", phone); + // sscanf(content, "%*[a-z]%11s", phone); + printf("%s\n", phone); + + return 0; +} \ No newline at end of file diff --git a/day12/d5_3.c b/day12/d5_3.c new file mode 100644 index 0000000..13cac27 --- /dev/null +++ b/day12/d5_3.c @@ -0,0 +1,18 @@ +// 提取内容 bc#def@ghi 中的 # 两边的内容 +// 都是贪婪匹配, 会一直读取到 # 为止 +#include +#include + +int main() +{ + char *content = "bc#def@ghi"; + char buf1[20] = ""; + char buf2[20] = ""; + // %[^#] 表示读取到 # 为止的内容, %[^@] 表示读取到 @ 为止的内容 + sscanf(content, "%[a-z]#%[a-z@]", buf1, buf2); + + printf("%s\n", buf1); + printf("%s\n", buf2); + + return 0; +} \ No newline at end of file diff --git a/day12/d5_4_test.c b/day12/d5_4_test.c new file mode 100644 index 0000000..f440e4b --- /dev/null +++ b/day12/d5_4_test.c @@ -0,0 +1,18 @@ +#include +#include + +int main() +{ + char *content = "disen:17791692095\njack:18799101234\nlucy:19088675432"; + char phone[3][12]; + // * 用来表示跳过内容 + sscanf(content, "%*[^:]:%s%*[^:]:%s%*[^:]:%s", phone[0], phone[1], phone[2]); + // sscanf(content, "%*[^0-9]%[0-9]%*[^0-9]%[0-9]%*[^0-9]%[0-9]", phone[0], phone[1], phone[2]); + // sscanf(content, "%*[^0-9]%s%*[^0-9]%s%*[^0-9]%s", phone[0], phone[1], phone[2]); + // sscanf(content, "%*[a-z:\n]%s%*[a-z:\n]%s%*[a-z:\n]%s", phone[0], phone[1], phone[2]); + + for (int i = 0; i < 3; i++) + printf("%s\n", phone[i]); // 循环打印 + + return 0; +} \ No newline at end of file diff --git a/day12/d6.c b/day12/d6.c new file mode 100644 index 0000000..1bb4a48 --- /dev/null +++ b/day12/d6.c @@ -0,0 +1,14 @@ +// const 修饰变量,表示只读变量 +#include + +int main() +{ + const int m = 10; + // m = 20; // 编译时报错: 因为 m 是只读变量 + + int *p = &m; // 地址可以赋值给指针变量 + *p = 20; // 可以通过指针变量修改只读变量的值 + + printf("%d\n", m); + return 0; +} \ No newline at end of file diff --git a/day12/d6_2.c b/day12/d6_2.c new file mode 100644 index 0000000..4830e28 --- /dev/null +++ b/day12/d6_2.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + const int m = 10; + + const int *p = &m; // 地址可以赋值给指针变量 + + int n = 50; + p = &n; // 可以修改指针变量的指向,指针从指向 m 变成指向 n + + printf("%d\n", *p); + return 0; +} \ No newline at end of file diff --git a/day12/d7.c b/day12/d7.c new file mode 100644 index 0000000..b711184 --- /dev/null +++ b/day12/d7.c @@ -0,0 +1,14 @@ +// const 修饰指针变量 p, 使得 p 变成只读变量, 不能指向新的地址 +// 指针变量是只读 (不能指向新的地址) +#include + +int main() +{ + const int n = 10; + int *const p = &n; // 必须初始化, 不等于 const int *p = &n; + *p = 20; // 可以修改指针变量指向的内容 + printf("%d\n", *p); + int m = 100; + // p = &m; // 编译时报错: 因为 p 是只读变量 + return 0; +} \ No newline at end of file diff --git a/day12/d8.c b/day12/d8.c new file mode 100644 index 0000000..c24e32a --- /dev/null +++ b/day12/d8.c @@ -0,0 +1,16 @@ +// const 修饰 * 和指针变量 + +#include +#include + +int main() +{ + int m = 100; + const int *const p = &m; // 第一个 const 修饰 *p, 第二个 const 修饰 p + *p += 10; // 编译时报错: 因为 *p 是只读变量 + int n = 90; + p = &n; // 编译时报错: 因为 p 是只读变量 + printf("%d\n", *p); + + return 0; +} \ No newline at end of file diff --git a/day12/d8_test1.c b/day12/d8_test1.c new file mode 100644 index 0000000..6afd449 --- /dev/null +++ b/day12/d8_test1.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + char m[30] = "lucy, 666"; + char *const p = m; // 必须初始化, 不等于 const char *p = m; + p[0] = 'd'; + *(p + 1) = 'o'; + printf("%s\n", p); + // p = "jack"; // 编译时报错: 因为 p 是只读变量 + + return 0; +} \ No newline at end of file