vi编辑器,编程,计算机常识部分见https://blog.csdn.net/m0_58053246/article/details/136525577
下面是词法符号部分:
关键字
存储类型
(决定变量存放的位置):auto(自动型) static(静态) extern(外部引用) register(寄存器) (c用不到,我们放到最后讲)
数据类型
(决定变量所占的空间大小):int(整型4) char(字符型1) short(短整型2) long(长整型4) float(单精度浮点型4) double(双精度浮点型8) signed(有符号) unsigned(无符号)
int a;//(signed)int a;
构造数据类型
(用户可自定义类型):struct(结构体) union(共用体) enum(枚举)
选择结构:
if else switch case default
循环结构:
for while goto do break continue
其他:
void(空类型 在函数、指针会见到) typedef(重定义 在结构体会见到,结构体名字比较长,可以起个小名) const(常量化 readonly 和void一起在指针会讲) sizeof(计算数据所占空间大小) volatile(防止编译器优化 ,平常用不到) return(返回值)
标识符
命名规则:
- 由字母、数字、下划线组成
- 开头不能是数字
- 不能和关键字重复
- 最好见名知意
练习:x y sum a100 _A7b_3x 3’a x*y @ b.8 while
运算符
算术运算符、逻辑运算符、位运算符、关系运算符、赋值运算符、三目运算符
算术运算符
+ - * / % ++ --
/整数相除,结果会向下取整
int a=5/2;
printf("%d\n",a); //2
float a=5/2;
printf("%f\n",a); //2.000000
float a=5/2.0;
printf("%f\n",a); //2.500000
float a=(float)5/2; //强制转换,只在当前这个位置进行转换
printf("%f\n",a); //2.500000
%只能用于整数之间的运算
10%3=1 5%2=1
++
独立成句:
int a=6;
1)++a; //a=a+1;
printf("%d\n",a); //7
2)a++; //a=a+1;
printf("%d\n",a); //7
赋值运算中:
int a=6;
1) int b=a++; //= +
printf("%d %d\n",a,b); a=7,b=6
2) int b=++a; //+ =
printf("%d %d\n",a,b); a=7,b=7
打印语句中:
int a=6;
1) printf("%d\n",a++); //6
2) printf("%d\n",++a); //7
练习:
1. 以下程序输出结果是
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 10;
int b = a++; //b=10,a=11
int c = a + b; //c=21
int d = (b++)+c; //d=10+21
printf("d = %d\n",d);
return 0;
} [单选题] *
A. d = 31
B. d = 32
C. d = 33
- d = 34
*********************************
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 10;
int b = a--; //b=10 a=9
int c = a + b + 2; //c=21
int d = (b--) + (++c); //d=10+22
printf("%d\n",d);
}
[单选题] *
A. d = 31
B. d = 32
C. d = 33
D. d = 34
1001=9
逻辑运算符
&& || ! 0假 1真
&&逻辑与: 全真则真,一假则假
||逻辑或 :一真则真,全假则假
!逻辑非:取反 非真即假,非假则真
printf("%d\n",15>9&&9>11); 0
printf("%d\n",15>9||9>11); 1
printf("%d\n",15>9&&!(9>11)); 1
截断法则:
逻辑与运算:当前边的表达式结果为假,后边表达式不再执行,直接返回假。
逻辑或运算:当前边的表达式结果为真,后边表达式不再执行,直接返回真。
#include <stdio.h>
int main()
{
int a=5,b=6,c=7,d=8,m=2,n=2;
(m=a<b)||(n=c>d);
printf("%d %d",m,n); //m=1,n=2
}
2
位运算符
& | ^ ~ << >> 二进制 0 1
&位与:全1则1,有0则0
|位或:有1则1,全0则0
^异或:相同为0,不同为1
~取反:0--》1 1--》0
int a=6,b=11;
int c=a&b;
//6: 110
//11:1011
//&: 0010==2
printf("%d\n",c);
整数的二进制表示有3种:
原码、反码、补码
整数在内存中存储是补码的二进制数
原码 | 反码 | 补码 | |
正数 | 本身 | 本身 | 本身 |
负数 | 本身 | 除符号位外,其他按位取反 | 反码+1 |
int a=2; int占4字节 1字节=8位 32位 0正数 1负数
int a=-6,b=11;
int c=a&b;
printf("%d\n",c);//10
先分别表示-6和11的补码:
-6原码:
1000 0000 0000 0000 0000 0000 0000 0110
-6反码:
1111 1111 1111 1111 1111 1111 1111 1001
-6补码:
1111 1111 1111 1111 1111 1111 1111 1010
11补码:
0000 0000 0000 0000 0000 0000 0000 1011
-6补码 & 11补码:
0000 0000 0000 0000 0000 0000 0000 1010==10
int a=5;
int b=~a;
printf("%d\n",b);//-6
5补码:
0000 0000 0000 0000 0000 0000 0000 0101
取反:
1111 1111 1111 1111 1111 1111 1111 1010
反码:
1111 1111 1111 1111 1111 1111 1111 1001
原码:
1000 0000 0000 0000 0000 0000 0000 0110==-6
<<左移
6<<2==24 6*2^2=24
-6<<2==-24 -6*2^2=-24
>>右移
11>>3==1 11/2^3=1
练习:
变量x和y的值相等,且为非零值,则以下选项中,结果为0的表达式是()
A.x||y B.x|y C.x&y D.x^y
课后练习
答案:1. A
2. 1 4 1 31 3
3.52
4.B