函数使用初体验
打印每个程序程序头,包括作者和联系方式等信息,一种使用函数一种不使用函数
1.用函数结构体程序如下
#include <stdio.h> #include <string.h> #define NAME "JISHABAO" #define Email "123456745678" #define Date "2016-09-19" #define WID 40 void show_name(char ch, int num); void show_no(const char * ); int main() { show_name('*',WID); show_no(NAME); show_no(Email); show_no(Date); show_name('*',WID); } void show_name(char ch, int num) //打印*函数 { int count; for(count=1;count<=num;count++) putchar(ch); printf("\n"); } void show_no(const char * p ) //打印信息函数 { int j; for(j=0;j<((WID-strlen(p))/2);j++) //信息居中设置 printf(" "); printf("%s\n",p); }2.不使用函数
#include <stdio.h> #include <string.h> #define NAME "JISHABAO" #define Email "123456745678" #define Date "2016-09-19" #define WID 40 void show_name(char ch, int num); void show_no(const char * ); int main() { int i; show_name('*',WID); for(i=0;i<((WID-sizeof(NAME))/2);i++) printf(" "); printf("%s\n",NAME); for(i=0;i<((WID-sizeof(Email))/2);i++) printf(" "); printf("%s\n",Email); for(i=0;i<((WID-sizeof(Date))/2);i++) printf(" "); printf("%s\n",Date); show_name('*',WID); printf("\n"); } void show_name(char ch, int num)<span style="white-space:pre"> </span>//打印*函数 { int count; for(count=1;count<=num;count++) putchar(ch); printf("\n"); }
对比一下使用函数与不使用函数的区别可以发现,用函数可以使程序更加精炼,看起来更符合逻辑结构。
打印结果如下
**************************************** JISHABAO 123456745678 2016-09-19 ****************************************
一个行者的旅途