变量和数据类型
1. 计算机内存
RAM
2.变量
变量是计算机中一块特定的内存空间。变量名只能由字母、数字和下划线组成;第一个字符必须为字母或者下划线;不能是保留字(关键字)。
类型保留字:
int,long,short,float,double,char,unsigned,signed,const,void,volatile,enum,struct,union
语句定义保留字:
if,elsa,goto,switch,casa,do,while,for,continue,break,return,default,typedef
存储类说明保留字:
auto,register,extern,static
长度运算符保留字:
sizeof
3.数据类型
3.1
单精度浮点型 float
float chang = 150.0f; //长
float kuang = 25.0f; //宽
float s = chang * kuang; //面积
printf("长方形的面积为:%.f\n",s);
return 0;
双精度浮点型 double
double radius = 2.5; //圆的半径
double area = 3.141592653 * radius * radius; //面积
printf("圆的半径为%.lf,面积为%.2lf\n",radius,area);
综合
int damage = 132; //命中伤害
int Bulletvelocity = 910; //初始子弹速度
int impactforce = 40000; //击中身体冲击力
double magazine = 4.600; //重填弹匣持续时间(填满)
int rds = 5; //弹匣容量
double Shootinginterval = 1.850; //射击间隔
printf("武器名称:AWM\n");
printf("命中伤害:%d\t\t初始子弹速度:%d\n",damage,Bulletvelocity);
printf("击中身体冲击力:%d\t重填弹匣持续时间(填满):%.3lf\n",impactforce,magazine);
printf("弹匣容量:%d\t\t射击间隔:%.3lfs\t\t射击模式:单发\n",rds,Shootinginterval);
字符型 char
char ch1 = 'a'; //小写字母a
char ch2 = 'A'; //大写字母A
char ch3 = ' '; //空格
printf("字符\tASCII码\n");
printf("%c\t%d\n",ch1,ch1);
printf("%c\t%d\n",ch2,ch2);
printf("%c\t%d\n",ch3,ch3);
printf("%c\t%d\n",ch1 - 32,ch1 - 32);
3.2
/*
武器实际最大伤害 = 武器最大伤害 * (玩家力量+100)/100
武器最大伤害999,要求输入玩家力量,打印武器实际最大伤害
*/
int lilanng ; //力量
double factshanghai ; //武器实际最大伤害
double maxshanghai = 999; //武器最大伤害
printf("请玩家输入当前角色力量:");
scanf("%d",&lilanng); //&取变量的地址 scanf只有占位符没有中文
factshanghai = maxshanghai * (lilanng + 100) / 100;
printf("武器实际最大伤害是%.2lf",factshanghai);
3.3 取模
int num1 = 5, num2 = 2;//定义两个整数变量
double result1,result2,result3,result4;
result1 = num1 / num2; //除法
result2 = num1 % num2; //取模
result3 = num1++; //后置
result4 = - -num2; //前置
printf("num1 / num2 = %lf\n",result1);
printf("num1 %% num2 = %lf\n",result2); //%%打印 %
printf("num1++ = %lf\n",result3);
printf("- -num2 = %lf\n",result4);
运算符和表达式
int %d; double %lf float %f char %c
csanf(%f,&num);
int num = 5/ 2; = 2
doublenum = 5/2; =2.0
自动类型转换
short>int>long>float>double
int num = 10;
num++; //num = num +1; 等价写法
num = num + 5; //num += 5; 等价写法
printf("%d\n",num);