第一章 程序设计和c语言

c语言例题及课后习题总结

第一章 程序设计和c语言

例题1.1

输出 This is a C program.
#include<stdio.h>
int main()
{
printf("This is a C program.\n");
return 0;
}

运行结果:
第一章 程序设计和c语言
例题1.2

求两个整数之和。
#include<stdio.h>
int main()
{
int a,b,sum;
a=123;
b=456;
sum=a+b;
printf("sum is %d\n",sum);
return 0;
}

运行结果:
第一章 程序设计和c语言
例题1.3

求两个整数中的较大者。
#include<stdio.h>
int main()
{
int max(int x,int y);
int a,b,c;
scanf("%d,%d",&a,&b);
c=max(a,b);
printf("max=%d\n",c);
return 0;
}

运行结果:
第一章 程序设计和c语言
课后习题
1.编写一个c程序,运行时输出 Hello word!

#include<stdio.h>
int main()
{
	printf("Hello word!");
	return 0;
}

运行结果:
第一章 程序设计和c语言
2.编写一个c程序,输出以下图形。
第一章 程序设计和c语言

#include<stdio.h>
int main()
{
	printf("*****\n  *****\n    *****\n      *****\n");
	return 0;
}

运行结果:
第一章 程序设计和c语言
3.编写一个程序,运行时输入a,b,c三个值,输出其中值最大者。

#include<stdio.h>
int main()
{
	int a,b,c,temp,max;
	scanf("%d,%d,%d",&a,&b,&c);
	temp=(a>b)?a:b;
	max=(temp>c)?temp:c;
	printf("a,d,c中最大数为:%d",max);
	return 0;
}

运行结果:
第一章 程序设计和c语言