一,类型转换运算
#include<stdio.h>
int main()
{
int num = 4;
int num1 = 3;
float div = num / num1; //隐式转换,数据类型向精度高的转化
float div1 = (float)num / num1; //显式转换即强制转换数据类型
printf("隐式转换%f\n", div);
printf("显式转换%f\n", div1);
printf("%f, %d", (float)num, num);//强制转换只是临时转换
return 0;
}
二,位运算
- 位与:&
- 位或:|
- 非:~
- 异或:^
- 左移:<<
- 右移:>>
三,赋值操作符
- =:在C里面是赋值(等号为==),比如int num = 0;
- 其他: +=,-=,*=,/=,&=,|=,^=,<<=,>>=,其中+=可以应用为num +=2;等同于num = num + 2;其他用法一样。
四,三目运算符
- Ready ? Go : Go on
a>b?a:b; //两个数求最大值
a>b?(a>c?a:c):(b>c?b:c); //三个数求最大值
五,if语句
#include<stdio.h>
int main()
{
int grade = 0;
printf("input grade: ");
scanf("%d", &grade);
printf(this grade is );
if(grade > 100 || grade <0) //if括号后没有“;”
printf("not grade!\n");
else if(grade >= 90) //下面的else if括号后均不能有“;”
printf("A\n");
else if(grade >= 70)
printf("B\n");
else if(grade >= 60)
printf("C\n");
else if(grade >= 0)
printf("D\n");
return 0;
}
六,switch语句
#include<stdio.h>
int main()
{
int grade = 0;
printf("input grade: ");
scanf("%d", &grade);
printf(this grade is );
grade /=10; //对成绩进行处理
switch(grade) //括号后不能有“;”
{
case 10:
case 9:
printf("A\n");
break; //若执行这语句跳出循坏
case 8:
case 7:
printf("B\n");
break;
case 6:
printf("C\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("D\n"); //对于多事件输出同一结果的问题可以这样处理
break;
default:
printf("not grade!\n");
break;
}
return 0;
}
七,while语句
#include<stdio.h>
int main()
{
int num = 0;
int sum = 0;
while(num <= 100) //while语句后不能有“;”
{
num++;
if(num % 10 == 4)
continue; //如果if为真,结束本次循环
sum +=num;
}
printf("和为 %d", sum)
return 0;
}
八,do while语句
#include<stdio.h>
int main()
{
int num = 0;
int sum = 0;
do
{
num++;
if(num % 10 == 4)
continue; //如果if为真,结束本次循环
sum +=num;
}
while(num <= 100); //do while 括号后“;”不能少
printf("和为 %d", sum)
return 0;
}
九,for语句
#include<stdio.h>
int main()
{
int num = 0;
int sum = 0;
for(num = 1; num <= 100; num++) //括号后没有“;”,括号内“;”不能少
sum +=num;
printf("1 + 2 +...+ 99 +100 = %d", sum);
return 0;
}