运算符、表达式和语句
循环简介
while (){
/code/
}
基本运算符
赋值运算符:=
bmw = 200; //从左往右赋值
//bmw 指定一个对象,引用内存中的地址 可修改的左值
//200 能赋值给可修改左值的量
加法运算符:+
a = b + d;
a = 5 + 6; //+和-都被称为二元运算符(binary operator)即需要两个运算对象才能完成
减法运算符:-
m = 6 - 2;
符号运算符:-和+
//符号运算符被称为一元运算符(unary operator)
a = 6;
b = -a;
c = +12;
乘法运算符:*
cm = 2.54 * inch;
除法运算符:/
four = 12.0/3.0; //分母小数表示 否则只能整除 分子只加小数表示浮点数输出为0
运算符优先级
运算符 | 结合律 |
---|---|
() | 自左往右 |
±(一元) | 自右往左 |
* / | 自左往右 |
+ - (二元) | 自左往右 |
优先级和求值顺序
// 优先级测试
#include <stdio.h>
int main(){
int top,score;
top = score = -(2+5) * 6+(4+3*(2+3));
printf("top = %d,score = %d\n",top,score);
return 0;
}
top = -23,score = -23
其他运算符
sizeof运算符和size_t类型
// 使用sizeof运算符
#include <stdio.h>
int main(){
int n = 0;
// size_t intsize;
int intsize = sizeof (int);
printf("n = %d, n has %zd bytes; all ints have %zd \
bytes.\n",n,sizeof n,intsize);
return 0;
}
求模运算符:%
111 % 5 得1
11 % -5 得 -2
11% -2 得 1
递增运算符:++
// 前缀和后缀
#include <stdio.h>
int main(){
int a = 1,b = 1;
int a_post,pre_b;
a_post = a++;
pre_b = ++b;
printf("a a_post b pre_b \n");
printf("%1d %5d %5d %5d\n",a,a_post,b,pre_b);
return 0;
}
a a_post b pre_b
2 1 2 2
递减运算符:–
#include <stdio.h>
#define MAX 100
int main(){
int count = MAX +1;
while (--count > 0)
{
printf("%d bottles of spring water on the wall,\
%d bottles of spring water!\n",count,count);
printf("Take one down and pass it around,\n");
printf("%d bottles of spring water!\n\n",count -1);
}
return 0;
}
优先级
递增运算符和递减运算符都有很高的结合优先级,只有圆括号的优先级比它们高。
y=2,n=3;
m=(y+n++)*6=5*6=30
表达式和语句
表达式
由运算符和运算对象组成,每个表达式都有一个值。
-4+6
5 > 3
6 + (c = 3+8)
语句
一条语句相当于一条完整的计算机指令。在C中,大部分语句都以分号结尾。
语句可以改变值或调用函数
legs = 4 //表达式
legs = 4; //语句
; //空语句
while语句是一种迭代语句,也被称为结构化语句
复合语句(块)
是用花括号括起来的一条或多条语句,复合语句也称为块(block)。
//程序段1
index = 0;
while (index++ < 10)
sam = 10 * index +2;
printf("sam = %d\n",sam);
//程序段2 整个while语句被视为一条语句
index = 0;
while (index++ < 10)
{
sam = 10 *index +2;
printf("sam = %d\n",sam);
}
类型转换
一般在语句和表达式中应使用类型相同的变量和常量。但是,如果使用混合类型,C 不会像 Pascal那样停在那里死掉,而是采用一套规则进行自 动类型转换。
类型转换出现在表达式
unsigned、signed、char、short都会自动转为int,有必要还会转换为unsigned int(short 与 int大小相同时)。
涉及两种类型运算
两个值会分别转换成两种类型得更高级别。
类型级别
类型的级别从高至低依次是long double、double、float、unsigned long
long、long long、unsigned long、long、unsigned int、int。
赋值表达式语句中,计算最终结果会被转换成被赋值变量得类型。
作为函数参数传递时,char 和 short 会被转换为int,float转换为double。
1.目标类型是无符号整型,且待赋的值是整数时,额外的位将被忽略。
2.如果目标类型是一个有符号整型,且待赋的值是整数,结果因实现而异。
3.如果目标类型是一个整型,且待赋的值是浮点数,该行为是未定义 的。
强制类型转换(cast)
mice = 1.6 + 2.7;
mice = (int)1.6 + (int)2.7;
带参数的函数
// 一个带参数的函数
#include <stdio.h>
void pound(int n); //ANSI函数原型声明 n为形参
int main(){
int times =5;
char ch = '!';
float f =6.0f;
pound(times); // times、ch、f都是实参要传递给对应的形参n
pound(ch);
pound(f); //pound((int)f)
return 0;
}
void pound(int n){ //表明函数接受一个int类型参数
while (n-- > 0)
printf("#");
printf("\n");
}
示例程序
#include <stdio.h>
const int S_PER_M = 60;
const int S_PER_H = 3600;
const double M_PER_K = 0.62137;
int main(){
double distk,distm;
double rate;
int min,sec;
int time;
double mtime;
int mmin,msec;
printf("This program converts your time for a metic race\n \
to a time for runing a mile and to your average\n \
speed in miles per hour.\n");
printf("please enter in kilometers,the distance run.\n");
scanf("%lf",&distk);
printf("Begin by entering the minutes.\n");
scanf("%d", &min);
printf("Now enter the seconds.\n");
scanf("%d",&sec);
time = S_PER_M * min + sec;
distm = M_PER_K * distk;
rate = distm / time * S_PER_H;
mtime = (double) time / distm;
mmin = (int) mtime / S_PER_M;
msec = (int) mtime % S_PER_M;
printf("You ran %1.2f km (%1.2f miles) in %d min, %dsec.\n",
distk, distm, min, sec);
printf("That pace corresponds to running a mile in %d min, ",mmin);
printf("%d sec.\nYour average speed was %1.2f mph.\n", msec, rate);
return 0;
}
复习题
1.假设所有变量的类型都是int,下列各项变量的值是多少:
y = 3 + 2*(x = 7/2);
x =3 y =9
x = (int)3.8 + 3.3;
x=3+3.3=3+3=6
x = 3 / 5 * 22.0;
x = 0 * 22.0 =0
x = (2 + 3) * 10.5;
x = 5 *10.5 =52.5 = 52
2.对下列各表达式求值:
a.30.0 / 4.0 * 5.0;
7.5*5.0
b.30.0 / (4.0 * 5.0);
30.0/2.0
c.30 / 4 * 5;
7*5
d.30 * 5 / 4;
150/4
e.30 / 4.0 * 5;
0
f.30 / 4 * 5.0;
7*5.0