1、第2章 数据类型、运算符与表达式南京审计学院南京审计学院信息科学与技术学院信息科学与技术学院孙玉星孙玉星 111/11/20222/9811/11/20223/98Preprocessor InstructionPengisytiharan globlmain()Pengisytiharan setempatStatementGlobal DeclarationLocal Declaration11/11/20224/9811/11/20225/9811/11/20226/9811/11/20227/9811/11/20228/9811/11/20229/9811/11/202210/9811
2、/11/202211/98Example:11/11/202212/9811/11/202213/98Example:number1?number2?25232311/11/202214/98Algorithm Syntax Rules:Expressions type must be the same as variables typeValid Example:Invalid Example:11/11/202215/98Example:Calculate and display the price of a number of apples if the quantity in kg a
3、nd price per kg are given.Input:and Output:Process:=*11/11/202216/98Example:11/11/202217/98Example:quantity?pricePerkg?price?4.509.00211/11/202218/98Example:11/11/202219/9811/11/202220/98数据类型数据类型基本类型基本类型构造类型构造类型指针类型指针类型空类型空类型整型整型实型(浮点型)实型(浮点型)字符型字符型枚举类型枚举类型数组类型数组类型结构体类型结构体类型共用体类型共用体类型单精度实型双精度实型基本整型长
4、整型短整型无符号整型11/11/202221/9811/11/202222/9811/11/202223/9811/11/202224/9811/11/202225/9811/11/202226/9811/11/202227/9811/11/202228/9811/11/202229/9811/11/202230/9811/11/202231/98#include main()printf(Data type Number of bytesn);printf(-n);printf(char%dn,sizeof(char);printf(int%dn,sizeof(int);printf(sho
5、rt int%dn,sizeof(short);printf(long int%dn,sizeof(long);printf(float%dn,sizeof(float);printf(double%dn,sizeof(double);11/11/202232/98阶码符号阶码符号阶码的数值阶码的数值尾数符号尾数符号尾数的数值尾数的数值阶码阶码j尾数尾数S11/11/202233/9811/11/202234/9811/11/202235/9811/11/202236/98b,98B,66b,98B,6611/11/202237/9811/11/202238/9811/11/202239/98
6、area=88.247263circumference=33.30085411/11/202240/9811/11/202241/9811/11/202242/9811/11/202243/98Example:11/11/202244/9811/11/202245/9811/11/202246/9811/11/202247/98Example:浮点数除法浮点数除法Floating Division or or both are floats整数除法整数除法Integer Division and are integers11/11/202248/98Example:11/11/202249/9
7、8Example:11/11/202250/98Example:11/11/202251/9811/11/202252/9811/11/202253/98Example:1252102remainderresult11/11/202254/9811/11/202255/98Example:-73-2-6-1remainderresult11/11/202256/98Example:7-3-261remainderresult11/11/202257/98Example:11/11/202258/9811/11/202259/98Examples:11/11/202260/9811/11/202
8、261/98Example:11/11/202262/98Example:11/11/202263/9811/11/202264/9811/11/202265/98#include main()float price,discount,total;printf(“Buying price:“);scanf(“%f”,&price);printf(“nDiscount rate:“);scanf(“%f”,&discount);total=price*discount;printf(“The total price is%.2fn”,total);Buying price:_discount?p
9、rice?total?Buying price:10.00_10.00Buying price:10.00Discount rate:_Buying price:10.00Discount rate:0.25_0.252.50Buying price:10.00Discount rate:0.25_Buying price:10.00Discount rate:0.25The total price is 2.50_Example:11/11/202266/9811/11/202267/98Example:int number,total;float start_x,start_y;.numb
10、er=total=0;start_x=start_y=100.0;total?number?start_x?start_y?00100.0100.011/11/202268/9811/11/202269/98Example:num=num+5;num152011/11/202270/98Example:num+=5;11/11/202271/9811/11/202272/9811/11/202273/98doublefloatlongunsignedintchar,short低高11/11/202274/9811/11/202275/98Example:int x=10;float y;y=(
11、float)x;x10y?10.00000011/11/202276/98Example:int total,number;float average;average=total/number;total15number2average?7.000000两个整数运算的结果两个整数运算的结果还是整数,不是浮点数还是整数,不是浮点数11/11/202277/98Example:int total,number;float average;average=(float)total/number;total15number2average?7.50000011/11/202278/9811/11/20
12、2279/98a b=cd=a bch a+1 d=a+b c3=x b)=cd=(a b)ch (a+1)d=(a+b)c)(3=x)a)a2b5c15d1711/11/202283/98Example:(a=1)&(b=5)a2b5c15d1711/11/202284/98Example:(c =(b*3)|(a=3)a2b5c15d1711/11/202285/98Example:!(a d)a2b5c15d1711/11/202286/9811/11/202287/9811/11/202288/9811/11/202289/98Example:j=+i-2i5similar to j?4611/11/202290/98Example:j=i+-2i5similar to j?3611/11/202291/9811/11/202292/9811/11/202293/9811/11/202294/9811/11/202295/9811/11/202296/9811/11/202297/98 0 1 1 65523 12 8 -13 12 8逻辑非的结果逻辑非的结果不是按位取反不是按位取反正数的补码正数的补码与其原码相同,与其原码相同,负数的补码为负数的补码为其反码加其反码加1