C 运算符、表达式和语句

1.一个示例程序

示例程序
//一个对长跑运动员有用的程序
#include<stdio.h>
#define S_PER_H  3600
#define S_PER_M  60
#define S_PER_K   0.62137
int main(void)
{
    double distk,distm;//跑过的路程,公里、英里
    double  rate;//以英里/每小时的单位平均速度
    int min,sec;//跑步用的分钟数和秒钟数
    int time;//用秒表示跑步时间
    double mtime;//跑完以英里所用的时间,以秒计算
    int mmin,msec;//跑完一英里所用的时间,以分钟和秒计算。
    printf("This program converts your time for a metric race
");
    printf("to a time foe running a mile and to your average
");
    printf("speed in miles per hour.
");
    printf("please enter ,in kilometers,the distance run.
");
    scanf("%lf",&distk);//表示读取一个double类型的数值
    printf("Next enter the time in minutes and distance and seconds.
");
    printf("Begin by entering the minutes.
");
    scanf("%d",&min);
    printf("Now enter the seconds.
");
    scanf("%d",&sec);
    //把时间转化为全部用秒表示
    time=S_PER_M*min+sec;
    //把公里转化为英里
    distm=S_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 can %1.2f km(%1.2f miles) in %d min.%d sec.
",
        //如果(%1.2f miles)变成了(%1.2 miles)会怎么样?
        distk,distm,min,sec);
    printf("That pacecorresponds to running a mile in %d min.",min);
    printf("%d sec.
Your average speed was %1.2f mph.
",msec,rate);
    return 0;
}
View Code

2.基本运算符

(1)赋值运算符:=

  • bmw=2002;    //将值2002赋给变量bnw,动作从右到左
  • 赋值运算左边必须指向一个存储位置(变量名——指针)

(2)加法、减法运算符:+、—(二元或双目运算符)

  • printf("%d",4+20);  //打印24    takehome=224.00-24.00;
  • 被加的值(操作数)可以是变量也可以是常量

(3)符号运算符:+和—(一元)

  • a=-12;b=-a;    //把值12赋给b.(只需要一个操作数)

(4)乘法运算符:*

  • cm=2.54*inch*inch;//用2.54的值乘以变量的,然后将结果赋给cm.
关于棋盘麦粒的问题
//关于棋盘麦粒的问题
#include<stdio.h>
#define SQUARES  64
#define CROP  1E15
int main(void)
{
    double current,total;
    int count=1;//棋盘的格子数
    printf("square grains added    total grains   fraction of
");
    printf("                                      US total
");
    total=current=1.0;//开始时是1粒  
    //total为麦子的总数
    //current为当前棋盘格子麦子的数量
    printf("%4d %13.2e %12.2e %15.2e
",count,current,total,total/CROP);
    while(count<SQUARES)
    {
        count=count+1;
        current=2.0*current;
            //下个方格的粒数加倍
        total=total+current;//更新总数
        printf("%4d %13.2e %12.2e %15.2e
",count,current,total,total/CROP);
    }
    printf("That's all.
");
    return 0;
}
关于棋盘麦粒的问题

(5)除法运算符:/

  • for=12.0/3.0;   //左边的值被右边的值除,把值4.0赋给for
  • 截尾:整数除法小数部分被丢弃。5/4的结果是1.(C99趋零截尾)
  • 混合类型:运算之前将整数转化为浮点数
除法运算符/
//除法运算符/
#include<stdio.h>
int main(void)
{
    printf("integer division 5/4 is %d 
",5/4);//整数除法的小树部分被丢弃
    printf("integer division 6/3 is %d 
",6/3);
    printf("integer division 7/4 is %d 
",7/4);
    printf("floating division 7.0/4. is %.2f 
",7.0/4.);
    printf("mixed(混合类型) division 7.0/4 is %.2f 
",7.0/4); //4自动转换成float类型
    return 0;
}
除法运算符

(6)取模运算符:%

  • 用于整数运算,对浮点数运算无效
  • a%b=a-(a/b)*b
  • 取模运算的结果符号与第一个操作数的符号一致
把秒转换成分钟和秒
//把秒转换成分钟和秒
#include<stdio.h>
#define SEC_PER_MIN  60
int main(void)
{
    int sec,min,left;
    printf("convert seconds to minutes and seconds!
");
    printf("Enter the number of seconds (<=0) to quit):
");
    scanf("%d",&sec);//读入秒数
    while(sec>0)
    {
        min=sec/SEC_PER_MIN;//截尾后得到的分钟数。
        left=sec%SEC_PER_MIN;//剩下的秒数。
        printf("%d seconds is %d minutes,%d seconds.
",
            sec,min,left);
        printf("Enter next value(<=0 to quit):
");
        scanf("%d",&sec);
    }
    printf("Done!
");
    return 0;
}
把秒转换成分钟和秒

(7)增量和减量运算符:++和——(只影响一个变量)

  • 前缀和后缀模式的区别在于值的改变这一动作发生的准确时间是不同的。
  • a++:先使用a,然后将它的增加;++a:先将a的值增加,然后再使用它。
  • shoe=3.0;while(shoe<10){...++shoe;}和shoe=2.0;while(++shoe<9){...}等价。
  • 替换方式b=++i;  ### ++i;b=i;//如果第一行使用了i++,b的结果仍然相同。
后缀和前缀
#include<stdio.h>
int main(void)
{
    int a=1,b=1;
    int aplus,plusb;

    aplus=a++;
    plusb=++b;
    printf("a aplus b plusb.
");
    printf("%ld %5d %5d %5d
",a,aplus,b,plusb);
    return 0;
}
//输出结果为 2 1 2 2
//a++ 使用a的值之后 改变a.
//++a 先改变a的值,然后使用a
后缀和前缀

(8)sizeof运算符和size_t类型:#、sizeof以字节为单位返回操作数的大小。#、允许为一个类型创建一个别名(typedef double  real;real a;//用real替代dobule)。
3.优先级

  • ()>+-(一元运算符)>*/%>+-(二元运算符)>=    +、-(一元)和=结合性从右向左。
  • y=6*12+5*20;除了两个运算符共享一个操作数的情况以外,C不保证复杂表达式的哪个部分首先被求值。
  • y=2;n=3;nextnum=(y+n++)*6;     //nexnum=(2+3)*6=5*6=30;n=4;

4.表达式和语句

四种类型的语句 
//四种类型的语句
#include<stdio.h>
int main(void)            //求出前20个整数的和
{
    int count,sum;        //声明语句
    count =0;             //赋值语句
    sum=0;
    while(count++<20)     //while
        sum=sum+count;    //语句
    printf("sum=%d
",sum);//函数语句
    return 0;
}
四种类型的语句

(1)表达式:表达式是运算符和操作数的组合,在C里每个表达式都有一个值。5>3(值为1),{6+(c=3+8)}#值为17。

(2)语句:用分号标识。

  • a=4是一个表达式,a=4;是一条语句。
  • int a=0,b  (如果从声明语句里去掉一个;则他既不是表达式也不具有一个值。
  • 副作用和顺序点。states=50;这个表达式的副作用是把变量states的值改变为50。语句里的分号标志了一个顺序点。
  • 复合语句用{};整个符合语句被认为是一个语句。缩排对编译器不起作用。

5.类型转换

  • 提升和降级(包含两种数据类型的运算中,两个值被转换成两种类型里较高的的级别),提升是平滑的无损害过程,降级会出现错误。
  • 避免自动类型转换特别是降级。
  • 指派运算符:【mice=1.6+1.7;//mice=3.3】【mice=(int)1.6+(int)1.7;//mice=2】

6.带有参数的函数

定义一个有参数的函数
//定义一个有参数的函数
#include<stdio.h>
void pound(int n);//函数声明(原型)
int main(void)
{
    int times=5;
    char ch='!';
    float f=6.0;
    pound(times);//int 型参数
    pound(ch);//char 参数自动转换成int类型
    pound((int)f);
    return 0;
}
void  pound(int n)//说明该函数接受一个int型参数
{
    while(n-->0)
        printf("#");
    printf("
");
}
定义一个有参数的函数
  • 参量是变量,而参数是由函数调用提供的值,并且将它赋给相对应的参量。
  • 函数原型声明  void pound (int n);//函数返回值为空,int 类型大参数。

附加部分~

#所有变量为int类型  则【x=(2+3)*10.5;】x=5*10.5=52(52.5截尾)@.@区别于x=5*10=50(x)

#把n除以k所得的余数赋给m:m=n%k;

#用b减去a的差去除q,并将结果赋给p:p=q/(b-a)@.@请注意去除和除以的区别

#define A "%s ! C is cool!"...printf(A,A);@.@等价于printf("%s ! C is cool!","%s ! C is cool!");//把"%s ! C is cool!"作为前面%s的参数。输出结果为【%s ! C is cool!! C is cool!】

原文地址:https://www.cnblogs.com/dondre/p/4082994.html