实验二 数据类型、运算符和表达式
1、
//分析字符、整型数据类型变量的不同
#include
void main()
{
char c1,c2;
c1 = 'a';
c2 = 'b';
printf("%c %c \n", c1, c2);
printf("%d %d \n", c1, c2);
}
2、
//利用sizeof(数据类型)测量基本数据类型长度
#include
void main()
{
//基本数据类型
printf("sizeof(char) : %d\n", sizeof(char));
printf("sizeof(short) : %d\n", sizeof(short));
printf("sizeof(int) : %d\n", sizeof(int));
printf("sizeof(long) : %d\n", sizeof(long));
printf("sizeof(float) : %d\n", sizeof(float));
printf("sizeof(double) : %d\n", sizeof(double));
}
运行截图为:
3、
//分析变量a,b,c,d的值
#include
int main()
{
int a,b,c,d;
a = 10;
b = a++;
c = ++a;
d = 10*a++;
printf(" a = %d \n b = %d\n c = %d\n d = %d\n", a, b, c, d);
return 0; //此处加return是因为int main,返回值是整型,如果是void main则不用加return,因为void返回的空。
}
4、
//分析复合赋值表达式的值
#include
int main()
{
int a = 10, b = 0;
b=a+=a;
printf("a=%d b=%d\n", a,b);
b=a-=a;
printf("a=%d b=%d\n", a,b);
a=10;
b=a*=2+5;
printf("a=%d b=%d\n", a,b);
a=10;
b=a%=4;
printf("a=%d b=%d\n", a,b);
a=10;
b=a+=a-=a*=a;
printf("a=%d b=%d\n", a,b);
a=10;
b=(a=10, a%5, a+3);
printf("a=%d b=%d\n", a,b);
a=10;
b=(a>10)?(a=12):(a=15);
printf("a=%d b=%d\n", a,b);
a=10;
b=a+++a;
printf("a=%d b=%d\n", a,b);
a=10;
b=a+++a++;
printf("a=%d b=%d\n", a,b);
return 0;
}
运行截图为: