实验二 数据类型和运算符
一、实验目的
1.掌握各种数据类型的定义方法。
2.掌握short int、int 类型的表示范围。
3.掌握C语言的运算符及包含这些运算符的表达式。
二、实验学时
三、实验内容
1.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
char ch='A';
//以十进制整数,输出ch
printf("%d\n" ,ch);
//以字符的形式,输出ch
printf("%c\n",ch);
ch=ch+32;
printf("%d\n" ,ch);
printf("%c\n",ch);
char c =ch+25;
//以十进制整数,输出c
printf("%d\n",c);
//以字符的形式,输出c
printf("%c\n",c);
int a=c;
printf("%d\n",ch);
return 0;
}
2.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
short int a=1; //占2个字节 二进制:00000000 00000001
unsigned short int b; //占2个字节
a--; //a的二进制:00000000 00000000
//以short int(十进制)形式,输出a
printf("%hd\n",a);
b=a-1; //b的二进制:11111111 11111111
//按short int(十进制)形式,输出b
printf("%hd\n",b);
//按int型输出b:00000000 00000000 11111111 11111111
printf("%d\n",b);
//按unsinged short int(十进制)形式,输出b
printf("%hu\n",b);
return 0;
}
3.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
short int a=32766,b; //占2个字节 二进制:01111111 11111110
unsigned short int c; //占2个字节
b=a+1; //b的二进制:01111111 11111111
c=a+1; //c的二进制:01111111 11111111
//以short int形式,输出a
printf("%hd\n",b);
//以unsinged short int形式,输出c
printf("%hd\n",c);
b++; //b的二进制:10000000 00000000
c++; //c的二进制:10000000 00000000
//以short int形式,输出b
printf("%hd\n",b);
//以unsinged short int形式,输出b
printf("%hu\n",c);
return 0;
}
4.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
int a=5,b=-7;
int c,d,e;
float f;
c=a%2;
//以int形式,输出c
printf("%d\n",c);
d=a/2;
//以int形式,输出d
printf("%d\n",d);
f=a/2;
//以float形式,输出f
printf("%f\n",f);
f=a*1.0/2;
//以float形式,输出f
printf("%f\n",f);
e=b%2;
//以int形式,输出e
printf("%d\n",e);
return 0;
}
5.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
float f1,f2;
f1=5*6/3;
//以float的形式,输出f1
printf("%f\n",f1);
f2=5/3*6;
//以float的形式,输出f2
printf("%f\n",f2);
f1=5.0/3*6; //修改为:f1=5.0*6/3;
printf("%f\n",f1);
f2=5/(3*1.0)*6;
printf("%f\n",f2);
f1=12.0*3/(2+5);
printf("%f\n",f1);
f2=12.0*3/2+5;
printf("%f\n",f2);
return 0;
}
6.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
int x,a,b,c,d;
x=a=b=10;
//c=3:赋值表达式
d=25%(c=3);
//以int的形式,输出d
printf("%d\n",d);
//(b,c=3):逗号表达式
d=25/(b,c=3);
printf("%d\n",d);
//(5,b+1):逗号表达式
d=25%(5,b+1);
printf("%d\n",d);
//条件表达式
d=(b>5)?8:2;
printf("%d\n",d);
return 0;
}
7.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
int a=5,b=3,c,d;
a+=6*2;
printf("%d\n",a);
b*=++a+2;
printf("%d\n",b);
printf("%d\n",a);
c=a++*3;
printf("%d\n",c);
printf("%d\n",a);
return 0;
}
8.分析下面程序的运行结果,运行程序进行验证。
#include<stdio.h>
int main(){
int a=0,b=20,c=30,d;
//逻辑运算符!(非)、&&(并且)、||(或者)
//!逻辑非运算,如果a为非0,则!a=0,如果a为0,则!a=1
d=!b+c; //! 非运算,如果a为非0,则!a=0;如果a为0,则!a=1
printf("%d\n",d);
d=(3>2)+(!a+5);//d=1+(1+5)=1+6=7
printf("%d\n",d);
d= 2<=2 + 5>3 ;//d=2<=7>3=1>3=0
printf("%d\n",d);
d= c>2 && 6>5 || b!=20 ; //d=1
printf("%d\n",d);
return 0;
}
四、实验小结
在实验报告中记录各题源程序及运行结果,针对实验中遇到的问题及其解决方法、或尚未解决的问题、实验收获等,仔细撰写实验报告。