1. 在Dev Cpp 平台输出 “Hello World!” 语句。(请用CSDN的Markdown语法提交代码+运行结果的截图,后面的题目同理,不再重复)
#include <stdio.h>
int main()
{
printf("hello,world!");
return 0;
}
2. 在Dev Cpp平台上新建一个项目,仿照截图,输入程序代码。观察其输出。
#include <stdio.h>
void main()
{
int a = 1;
float b = 2.5;
char c = 'A';
printf("a = %d\n", a);
printf("b = %f\n", b);
printf("c = %c\n", c);
printf("End of program\n");
}
3 在Dev Cpp平台上新建一个项目,仿照截图,输入程序代码。观察其输出。(请用CSDN的Markdown语法提交代码+运行结果的截图)
#include <stdio.h>
main()
{
printf("Data type Number of bytes\n");
printf("------------- ---------------------\n");
printf("char %d\n",sizeof(char));
printf("int %d\n",sizeof(int));
printf("short int %d\n",sizeof(short));
printf("long int %d\n",sizeof(long));
printf("float %d\n",sizeof(float));
printf("double %d\n",sizeof(double));
return 0;
}
4. 输入书本P48「温度转换」的代码,并观察其输出。
#include <stdio.h>
int main( )
{
double temC, temF;
temF = 100;
temC = 5 * (temF - 32) / 9;
printf("华氏温度%5.2f对应的摄氏温度是%5.2f\n", temF, temC);
return 0;
}
5. 输入书本P52「三角形周长」的代码,并观察其输出。
#include <stdio.h>
int main( )
{
int a = 6, b =5, c = 5;
int triC;
triC = a + b + c;
printf("三角形的周长是:%d\n", triC);
return 0;
}
6. 溢出问题:
short short_value = 32767; // short占2个字节,最大值32767;short_value += 1;printf(“%d”, short_value);
上述结果输出值为:short_value = -32768, 请分析这个值是怎么得到的?
#include <stdio.h>
main ()
{
short short_value = 32767; // short占2个字节,最大值32767//;
short_value += 1;
printf("%d", short_value);
}
32767转化为二进制为0111 1111 1111 1111;
二进制0111 1111 1111 1111 + 1 = 1000 0000 0000 0000,而二进制首位表示正负,
求1000 0000 0000 0000的补码:取反为1111 1111 1111 1111,再加1为1 0000 0000 0000 0000,转化为十进制为-(2的十六次方)结果为-32768。
7. 求值:(a)-30 * 3 + 21 / 6
(b) -30 + 3 * 21 / 6
(c) 30 / 3 * 21 % 6
(d) -30 / 3 * 21 % 4
#include <stdio.h>
int main ()
{
int a = -30 * 3 + 21 / 6;
int b = -30 + 3 * 21 / 6;
int c = 30 / 3 * 21 % 6;
int d = -30 / 3 * 21 % 4;
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
}
8. 假设i,j, k 都是整数,说明表达式 i != j < k的含义。
由于运算符优先级"<"优先于"!=",据此先判断"j < k",再判断"i != j"。
9. 赋值完毕,i, d 的值分别为多少?
int i ; double d;
(a) d = i = 3.5;
(b) i = d = 3.5;
#include <stdio.h>
main()
{
int i;
double d;
d = i = 3.5;
printf("i=%d,d=%f\n",i,d);
i = d = 3.5;
printf("i=%d,d=%f",i,d);
}
10. 请查询资料了解「条件运算符」的语法、例子等。
条件运算符由"?"和":"组成,必须一起使用。(唯一一个三目运算符)
语法: 表达式1 ? 表达式2 : 表达式3
例子:
#include <stdio.h>
int main()
{
int i = 2, j = 3, k;
k = i > j ? i : j;
printf("k=%d\n",k);
k = (i>1 ? 5 : 8) + j;
printf("k=%d\n",k);
}
11. 假设有如下定义:
char cval;
int ival;
unsigned int ui;
float fval;
double dval;
请回答下列表达式是否又发生类型转换。请将有发生的情况指出来。
(a)cval = ‘a’ + 3;
否,仍为char。
(b)fval = ui – ival * 1.0;
是,转化为double。
(c)dval = ui * fval;
是,转化为double。
(d)cval = ival + fval + dval
否,仍为char。