C语言程序设计源代码.doc
第二章
L2-1a-1
main()
{
int a;
float b;
char c;
a = 1;
b = 2.5;
c = 'A';
}
L2-1a-2
main()
{
int a = 1; /* 定义整型变量a并对其初始化 */
float b = 2.5; /* 定义实型变量b并对其初始化 */
char c = 'A'; /* 定义字符型变量c并对其初始化 */
}
L2-1b
#include
main()
{
int a = 1;
float b = 2.5;
char c = 'A';
printf("a = %d\n", a); /* 按整型格式输出变量a的值 */
printf("b = %f\n", b); /* 按实型格式输出变量b的值 */
printf("c = %c\n", c); /* 按字符型格式输出变量c的值 */
printf("End of program\n"); /* 输出一行字符串 */
}
L2-2
#include
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));
}
L2-3
#include
main()
{
float a;
double b;
a = 123456.789e4;
b = 123456.789e4;
printf("%f\n%f\n",a,b);
}
第三章
L3-1
#include
main()
{
int x, b0, b1, b2, sum;
x = 153;
b2 = x / 100; /*计算百位数字*/
b1 = (x - b2 * 100) / 10; /*计算十位数字*/
b0 = x % 10; /*计算个位数字*/
sum = b2 + b1 + b0;
printf("b2=%d, b1=%d, b0=%d, sum=%d\n", b2, b1, b0, sum);
}
L3-2
#include
main()
{
double r = 5.3; /* 圆的半径 */
double circum; /* 圆的周长 */
double area; /* 圆的面积 */
circum = 2 * 3.14159 * r; /* 计算圆的周长 */
area = 3.14159 * r * r; /* 计算圆的面积 */
printf("circumference = %f\n", circum);
printf("area = %f\n", area);
}
L3-3
#include
main()
{
double r;
double circum;
double area;
printf("Input r:"); /* 提示用户输入半径的值*/
scanf("%lf", &r); /* 从键盘输入半