getchar()
可以读取缓冲器的一个字符,读取错误返回值为EOF(ctrl+z可以返回这个EOF)
可以用getchar()清理缓冲区的内容(需要适当修改)
需要用一变量将getchar里面的值读出来,否则将被下一个值覆盖,无法使用
1
2
3
4
5int tmp=0; while((tmp=getchar())!='n') { ; }
运算优先级
C语言运算符优先级(超详细)_yuliying的专栏-CSDN博客_运算符优先级
putchar()
打印一个字符
关于for()的两种种情形
for(;a<3;a++)如果在涉及到循环嵌套的情况,a依然还是2
for(a+0,b=5;a<5&&b>1;a++,b--)其中只要语句2中间判断前条件为假,此函数将结束循环
关于给结构体数组赋值的问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include<stdio.h> #include<string.h> struct test { char *name1; char name2[20]; }t1,t2; int main() { char * str; char str2[20] = "nice to meet you"; char *name3; char name4[20] = "test"; str = "hello word"; //特别的 *name4 = *str;//复制内容 printf("%sn", name4); //name1 t1.name1 = str;//交换首地址 printf("%sn", t1.name1); t1.name1 = str2;//交换首地址 printf("%sn", t1.name1); //name2 strcpy(t1.name2, str);//复制内容 printf("%sn", t1.name2); strcpy(t1.name2, str2);//复制内容 printf("%sn", t1.name2); //name3 name3 = str;//交换首地址 printf("%sn", name3); name3 = str2;//交换首地址 printf("%sn", name3); //name4 strcpy(name4, str);//复制内容 printf("%sn", name4); strcpy(name4, str2);//复制内容 printf("%sn", name4); //特别的 *name4 = *str;//复制内容 printf("%sn", name4); return 0; }
结构体里面字符串数组,则赋值的时候使用strcpy(string.h)
结构体里面是字符指针,则赋值直接用结构体成员(地址)=字符串(地址)
1
2
3
4
5
6
7
8
9
10
11输出结果 hest //替换了第一个元素 后面输出原来的 hello word nice to meet you hello word nice to meet you hello word nice to meet you hello word nice to meet you hice to meet you //替换了第一个元素 后面输出原来的 由于strcpy 数组内容改变
结构体内部定义函数
代码参考: 结构体函数作用及示例_dingdongkk~博客-CSDN博客_结构体函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include<stdio.h> #include<stdlib.h> /*structure declare*/ struct str_func{ int a; int b; int (*add)(int a, int b); int (*sub)(int a, int b); int (*compare)(int a, int b); }; int add(int a, int b){ return a+b; } int sub(int a, int b){ return a - b; } int compare(int a, int b){ if (a>b) return a; else return b; } /*create a structure and init*/ struct str_func test = { .a = 5, .b = 7, .add = add, //function pointer point to function .sub = sub, .compare = compare, }; int main(){ if (test.compare) printf("a b max = %dn",(test.compare(test.a,test.b))); if (test.compare) printf("a add b = %dn",(test.add(test.a,test.b))); if (test.compare) printf("a sub b = %dn",(test.sub(test.a,test.b))); return 0; } ———————————————— 版权声明:本文为CSDN博主「dingdongkk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/sinat_29891353/article/details/83067747
typedef的使用
typedef的用法,C语言typedef详解 (biancheng.net)
记住如果:
typedef char* Point;
const Point 不等同于const char* 而是等同于char *const(将指针变为常量指针,即指针的地址不可以变)
结构体运用指针返回值
定义一个函数体指针,指针地址不变,则不需要返回值,直接将定义结构体变量t1首地址赋给函数即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/*结构体使用测试*/ #include<stdio.h> struct AGE { int year; int month; int day; }birthday; typedef struct test { int id ; struct AGE birthday; char *name; }test2; void record(test2 *p,int year,int month,int day,int id,char *str ) { (*p).birthday.year = year; (*p).birthday.month = month; (*p).birthday.day = day; (*p).id = id; (*p).name = str; } void test1(test2 *a) { record(a, 1997, 05, 21, 152121, "楚云歌"); } int main() { test2 t1; test1(&t1); printf("%s %d %d/%d/%dn", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year); record(&t1, 1997, 05, 21, 152121, "楚云歌"); printf("%s %d %d/%d/%dn", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year); t1.birthday.year = 2222; t1.birthday.month = 5; t1.birthday.day = 21; t1.id = 1502121; t1.name = "chuyunge"; printf("%s %d %d/%d/%dn", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year); return 0; }
函数体整体返回值
定义一个和原来结构体类型相同的函数,形参类型为结构体内部成员类型,此时在使用时用一个同样结构体类型的结构体变量可以接收到结构体函数内部对于数值的处理,此时如果要封装到一个外部饮用的函数,可以看到的是,在外部函数可以使用,回到主函数内部则不可使用(函数生命周期结束,相关内存被释放,如果想达到效果,建议使用指针)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46/*结构体整体返回*/ #include<stdio.h> struct AGE { int year; int month; int day; }birthday; typedef struct test { int id ; struct AGE birthday; char *name; }test2; test2 record(int year,int month,int day,int id,char *str ) { test2 temp; temp.birthday.year = year; temp.birthday.month = month; temp.birthday.day = day; temp.id = id; temp.name = str; return temp; } void test1(test2 *a) { test2 t1; t1 = record(1997, 05, 21, 152121, "chuyunge"); printf("%s %d %d/%d/%dn", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year); a = &t1; printf("%s %d %d/%d/%dn", a->name, a->id, a->birthday.day, a->birthday.month, a->birthday.year); } int main() { test2 *t1=0x00000000; test2 t2; t2= record(1997, 05, 21, 152121, "楚云歌"); printf("%s %d %d/%d/%dn", t2.name, t2.id, t2.birthday.day, t2.birthday.month, t2.birthday.year); test1(t1); printf("%s %d %d/%d/%dn", t1->name, t1->id, t1->birthday.day, t1->birthday.month, t1->birthday.year); return 0; }
运行结果
1
2
3
4
5楚云歌 152121 21/5/1997 //打印结构体返回的数值 chuyunge 152121 21/5/1997 //在外部函数打印出来 chuyunge 152121 21/5/1997 //在外部函数中打印数值 //调用外部函数出现问题
指针直接定义在函数中,可以返回多个值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include<stdio.h> void test2(int a, int b, int* p1, int* p2, int* p3) { *p1 = a + b; *p2 = a - b; *p3 = a * b; } int main() { int* add = 0, * minus = 0, *times = 0; test2(2, 3, &add, &minus, ×); printf("add=%-4d minus=%-4d times=%-4dn", add, minus, times); return 0; }
此外话可以定义static和extern来得到想要返回的值
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include<stdio.h> extern int b = 0; static i = 0; int test1(int a) { a = 3; i = 4; b = 5; return 0; } int main() { test1(i); printf("%dn", i); test1(b); printf("%d", b); return 0; }
返回值是4 5;由此可见,把变量代入形参进去哪怕在外部函数内部中有这个形参被赋值,仍然不可以带回到主函数中,二在外部函数可以直接用这个变量,并给他赋值,可以带回到主函数中
最后
以上就是朴素奇异果最近收集整理的关于C语言学习笔记-知识点随记2的全部内容,更多相关C语言学习笔记-知识点随记2内容请搜索靠谱客的其他文章。
发表评论 取消回复