C语言学习日记第四篇
1. 结构体
1 #include <stdio.h> 2 3 struct Book 4 5 { 6 7 char name[20]; 8 9 int price; 10 11 12 13 }; 14 15 int main(){ 16 17 struct Book b1 = {"C语言", 55}; 18 19 printf("书名%s\n", b1.name); 20 21 printf("原价%d\n", b1.price); 22 23 b1.price = 15; 24 25 printf("修改后价格%d\n", b1.price); 26 27 return 0; 28 29 }结果为书名C语言 30 31 原价55 32 33 修改后价格15
2.指针型
1 #include <stdio.h> 2 3 struct Book 4 5 { 6 7 char name[20]; 8 9 int price; 10 11 12 13 }; 14 15 int main(){ 16 17 struct Book b1 = {"C语言", 55}; 18 19 struct Book *pb=&b1; 20 21 printf("书名%s\n",(*pb).name); 【printf("书名%s\n",pb->name); 22 23 printf("原价%d\n", (*pb).price); printf("原价%d\n",pb->price);】二者等价 24 25 return 0; pb->意思是指向成员 26 27 }结果为书名C语言 28 29 原价55
改变数组中的数据,且其中的成员是字符串要用strcpy(string copy字符串拷贝)且头文件要加上 #include <string.h>
1 #include <stdio.h> 2 3 #include <string.h> 4 5 struct Book 6 7 { 8 9 char name[20]; 10 11 int price; 12 13 14 15 }; 16 17 int main(){ 18 19 struct Book b1 = {"C语言", 55}; 20 21 struct Book *pb=&b1; 22 23 strcpy(b1.name, "math"); 24 25 printf("书名%s\n",pb->name); 26 27 printf("原价%d\n",pb->price); 28 29 return 0; 30 31 }
3.break;和continue;的区别
1 #include <stdio.h> 2 3 int main(){ 4 5 int ch = 0; 6 7 while ((ch = getchar()) != EOF) 8 9 { 10 11 if (ch<'0' || ch>'9') 12 13 break; //【continue;】break是终止循环,直接结束进程。而continue //putchar(ch); 则是终止本次循环,还会将值返回判断是否满足条件。 14 15 } 16 17 return 0; 18 19 }
浙公网安备 33010602011771号