C语言学习笔记1.doc
文本预览下载声明
Day01
数据类型:
(1)基本类型
(2)构造类型
(3)指针类型
(4)空类型
常量与变量:
1.常量:
直接常量
①整型
②实型
③字符型
(2)符号常量
一般格式:
#define 符号常量 常量值
例:
#includestdio.h
#define PAI 3.14159
void main(){
float r,s,l;
printf(Please input circle :);
scanf(%f,r);
s=PAI*r*r;
l=2*PAI*r;
printf(s=%f\nl=%f\n,s,l);
}
2.变量:
(1)标识符的命名规则
(2)命名习惯
整型数据
1.整型常量
(1)8进制
(2)16进制
(3)10进制
2.整型变量
(1)基本型 int
(2)短整型 short int或short
(3)长整型 long int或 long
(4)无符号型 unsigned int, unsigned short, unsigned long
例1:
[root@localhost C]# cat 3.c
#includestdio.h
void main(){
short int a,b; //short int ( -2^15~2^15-1)
a=32767; //32767=2^15-1
b=a+1; //数据存储量不够,溢出
printf(%d,%d\n,a,b);
}
[root@localhost C]# ./test3
32767,-32768 //b= -32768
例 2:
[root@localhost C]# cat 5.c
#includestdio.h
void main(){
int a,b,c,d;
unsigned u;
a=10;b=-20;u=30;
c=a+u;d=b+u;
printf(c=%d,d=%d\n,c,d);
}
[root@localhost C]# ./test5
c=40,d=10
实型数据
1.实型常量
(1)10进制
指数 e或E
2.实型变量
(1)单精度
(2)双精度
例:
[root@localhost C]# cat 4.c
#includestdio.h
void main(){
float a;
double b;
a=1234.56789;
b=1234.5678901234;
printf(a=%f\nb=%f\n,a,b);
}
[root@localhost C]# ./test4
a=1234.567871 //7位有效位
b=1234.567890 //16位有效位
Day02
字符型数据
1.字符常量 ‘x’ ----------- 内存1个字节 x
2.转义字符 \n
3.字符变量 char c1; c1=’a’;
4.字符串常量 “chen” chen\0 ; “x” ------------------- 内存2个字节 x\0
类型转换
1.自动转换
[root@localhost C]# cat 10.c
#includestdio.h
//auto change
void main(){
float PAI=3.1415;
int s,r=10;
s=PAI*r*r;//类型自动转换为float
printf(s=%d\n,s);
}
2.强制类型转换
[root@localhost C]# cat 11.c
#includestdio.h
void main(){
float f=3.51;
printf((int)f=%d,f=%f\n,(int)f,f);//强制类型转换
}
基本运算符和表达式
①.算术运算符和算数表达式
②.赋值运算符和复制表达式 x* =y+7 r%=p
③.逗号运算符和逗号表达式 x=8*2,x* 4
显示全部