第二天浅识c语言
1.常量
(1) 字面常量
1 #include2 int main(){ 3 3; 4 4; 5 5; //字面常量的意思就是能够直接写出的常量的意思,懂得都懂 6 7 return 0; 8 }
(2) const修饰的常变量
1 #include2 int main() { 3 int a =10; 4 printf("%d",a); 5 a = 20; 6 printf("%d",a);//这是属于正常变量的范畴 7 return 0; 8 }
1 #include2 int main() { 3 const int a =10; 4 printf("%d",a); 5 //a = 20; //这里会执行报错,原因是因为"const"已经限定了a的值,a 6 //的值不能再次被改变 7 //printf("%d",a); 8 return 0; 9 }
1 #include2 int main() { 3 const int a = 10; //经过const修饰后a具有"常属性",不能够对a进行修 4 //改 5 int arr[10] = {0}; 6 //const arr[a] = {0};//这里会执行报错,原因是数组中的参数不接受变量 7 //说明尽管a已经被修饰成常变量,依然是一个变量 8 return 0; 9 }
(3) define定义的标识符常量
1 #include2 #define num = 10; 3 int main() { 4 int arr[num] = {0};//由于数组的原因,num只能是常量,而这一行代码 5 //能够正常运行,也说明了"#define"定义的是一个常量 6 return 0; 7 }
(4) 枚举常量
1 enum color { 2 red, 3 yellow, 4 blue //枚举的意思就是意思就是列举 5 } 6 #include7 int main(){ 8 enum color x = red;//x为变量 9 x = blue; //变量可以被改变 10 //blue = 6; //枚举常量不可以被改变 11 return 0; 12 }
2.字符串
(1) 关于字符串的解释
1 #include2 int main(){ 3 "10"; 4 "55516"; 5 "你好!" //双引号的内容统称为字符串 6 return 0; 7 }
(2) 字符串的储存
1 #include2 int main() { 3 char arr1[] = "abc"; 4 char arr2[] = {'a','b','c','\0'};//字符串的储存需要创建数组,以上两种方式 5 //都可以,"\0"要加上是因为,在arr1中默认加 6 //上"\0","\0"是储存字符串结束的标志 7 printf("%s\n",arr1); 8 printf("%s\n",arr2); //%s是打印字符串的转换说明 9 printf("%d\n",strlen(arr1)); 10 printf("%d\n",strlen(arr2)); //strlen指的是字符串的长度 11 return 0; 12 }
3.转义字符
1 #include2 int main() { 3 printf("%d\n",32);//"\n"是换行的意思,而不是单纯的输出"\n" 4 char arr[] = {'a','b','c','\0'};//"\0"的意思是结束字符串储存的意思,而不 5 //是单纯的储存"\0" 6 //转义字符的意思就是转换意思的意思,懂得都懂 7 8 return 0; 9 }
1 #include2 int main(){ 3 printf("%d",strlen("c:\text\32\text.c")); 4 //"\t"代表着一个转义字符,因此长度只能算一个,"\32"也代表着3,2 5 //两个八进制数字,通过转换十进制后通过ascll码变成箭头的符号,因此也 6 //只能算一个字符 7 printf("%c",'\x86');//同理,但是是16进制的两个数字8,6 8 return 0; 9 }
4.if语句
1 #include2 int main() { 3 int input = 0; 4 printf("你要好好学习吗?\n 是/否(1/0):"); 5 scanf("%d",&input); 6 if(input==1){ 7 printf("迎娶白富美!"); 8 }else{ 9 printf("烤红薯"); 10 } 11 return 0; 12 }
5.while循环语句
1 #include2 int main() { 3 int sum = 0; 4 while(sum<20000){ 5 printf("写了一行代码,此代码为第%d行\n",sum); 6 sum++; 7 } 8 printf("迎娶白富美!"); 9 return 0; 10 }
6.函数
1 #include2 int add (int x,int y){//这是自定义函数,"int"是返回值类型,"add"是 3 //自定义函数名,"()"内的内容是函数参数 4 int z = 0; 5 z = x + y; 6 return z; 7 } 8 int main(){ 9 10 int sum = 0; 11 sum = add (4,5); 12 printf("%d",sum); 13 14 return 0; 15 }
7.数组
1 #include2 int main() { 3 int i = 0; 4 int a = 1; 5 int b = 2; 6 int c = 3; 7 int d = 5; 8 int e = 6; 9 //.......... 10 int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; 11 printf("%d\n", arr[0]); 12 while (i < 10) { 13 printf("%d ", arr[i]); 14 i++; 15 } 16 17 return 0; 18 }
8.算数操作符
1 #include2 int main() { 3 int a = 1; 4 int b = 2; 5 int c = 3; 6 int d = 4; 7 int sum = 0; 8 int reduce = 0; 9 int multiply = 0; 10 int except = 0; 11 int remainder = 0; 12 sum = a + b; 13 reduce = c - b; 14 multiply = b * c; 15 except = d / b; 16 remainder = c % b; 17 printf("%d\n", sum); 18 printf("%d\n", reduce); 19 printf("%d\n", multiply); 20 printf("%d\n", except); 21 printf("%d\n", remainder);
9.移位操作符
移位(>>,<<)操作符,移的是二进制位
例如:int是4个字节,有32位,举个例子:00000000000000000000000000000001--1
向左移一位:
1 #include2 int main() { 3 int a=1; 4 int c = a<<1; 5 printf("%d",c); 6 return 0; 7 }
结果为2 : 00000000000000000000000000000010--2
10.位操作符
“&”“|”“^”位操作符也指的是二进制
1 #include2 int main(){ 3 int a =3; 4 int b =4; 5 int c =0; 6 c = a & b; 7 printf("%d\n",c);------0 8 //a的二进制为:011; 9 //b的二进制为:100; 10 //a&b为: 000;---有假即为假 11 c = a | b; 12 printf("%d\n",c);------7 13 //a|b的二进制为:111;---有真即为真 14 c = a ^ b; 15 printf("%d\n",c);-------7 16 //a^b的二进制为:111;---同真同假即为假,异真异假即问真 17 18 return 0; 19 }
11.复合赋值符
1 #include2 int main() { 3 //复合赋值符:+=,-=,*=,/=,%=,&=,|=,^= 4 int a = 5; 5 int b = 6; 6 a *= 6;//-- a = a * 6; 7 printf("%d", a); 8 return 0; 9 }
12.单目操作符,双目操作符,三目操作符
1 #include2 int main() { 3 //单目操作符,双目操作符,三目操作符 4 //a + b;操作符左右有两个数字为双目操作符,只有一个为单目操作符.... 5 int a = 3; 6 int b = 4; 7 int c = 0; 8 int arr[10] = { 0 }; 9 printf("%d\n",!a); 10 printf("%d\n",sizeof(arr));//求数组大小 11 c = sizeof(arr) / sizeof(arr[0]);//求元素个数 12 printf("%d\n",c); 13 return 0; 14 }
1 #include2 int main() { 3 int a = 10; 4 int b = 0; 5 int ch[] = { 1,2,3,4,5,6 };//创建一个数组ch,数组元素的类型为int; 6 printf("%d\n", sizeof(a));//输出为4,单位字节 7 printf("%d\n", sizeof(int));//输出为4,单位字节 8 printf("%d\n", sizeof a);//输出为4,单位字节 9 //可以看出size是一个操作符而不是一个函数 10 //printf("%d",sizeof int );//报错,原因输出类型错误 11 printf("%d\n", sizeof(ch));//输出为24,单位字节 12 b = sizeof(ch) / sizeof(ch[0]);//输出结果为6 13 printf("%d\n",b); 14 return 0; 15 }