C语言代码大全

来源:互联网 发布:新视野网络教学系统 编辑:程序博客网 时间:2024/05/06 08:26
 

从今天开始,我将C语言的代码实例从发表在博客上。供技术交流。比较基础,遵循循序渐进的原则,由简单到复杂,为以后的程序员生涯打好基础。

1.编写一个C程序输出以下信息。

*****************
    Very Good!
*****************

代码如下:

# include <stdio.h>

int main(void) {
 printf("*****************\n") ;
 printf("    Very Good!   \n") ;
 printf("*****************\n") ;
 return 0 ;
}

2.求两个数中的较大者

代码如下:

# include <stdio.h>
int main(void) {
 int max(int x,int y) ;   //引入下面定义的函数max(int x,int y) ;
 int a,b,c ;      //定义变量
 scanf("%d,%d",&a,&b) ;   //键盘上输入参数
 c = max(a,b) ;     //将最大的值保存在变量C当中
 printf("%d\n",c) ;    //打印输出最大值
 return 0 ;
}

int max(int x,int y) {    //定义求最大值的函数
 int z ;
 if(x > y){      
  z = x ;
 } else {
  z = y ;
 }

 return z ;
}

 3.输入三个数,比较其中的最大值。

# include <stdio.h>
int main(void) {
 int max(int x,int y,int z) ;  //声明函数的名称
 int a,b,c,result ;     //定义变量
 scanf("%d,%d,%d",&a,&b,&c) ;  //从键盘上输入
 result = max(a,b,c) ;    //将最大的值的结果赋值给result
 printf("The max number is %d\n",result) ;//输出最大值
 return 0 ;
}

int max(int x,int y,int z) {   //处理最最大值的函数
 int temp ;
 temp = x ;
 if(temp < y) {
  temp = y ;
 } else if(temp < z) {
  temp = z ;
 }
 
 return temp ;
}