小白学编程7:代码复用,理解函数

《小白学编程》第七讲的相关代码:
#include <stdio.h>
int gd_square(int x)
{
return x*x;
}
int main()
{
int n = 88;
printf("square of %d is %d\n", n, gd_square(n));
return 0;
}
~
#include <stdio.h>
int main(int argc, const char* argv[])
{
printf("main received %d arguments\n", argc);
for(int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
#include <stdio.h>
int usage()
{
printf("gdplay %d.%d, a utility just for fun\n"
"\tgdplay [options]\n"
"\t supported options:\n"
"\t -h show this help\n"
"\t -t print time\n", VER_MAJOR, VER_MINOR);
}
void gd_time()
{
}
int main(int argc, const char* argv[])
{
printf("main received %d arguments\n", argc);
for(int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
if(argc <= 1)
return usage();
if(argv[1][0] == '-') {
switch(argv[1][1]) {
case 'h':
return usage();
case 't':
gd_time();
break;
}
}
return 0;
}
#define VER_MAJOR 0
#define VER_MINOR 1
geduer@gdk8:~/clabs$ cat lab5-3.c
#include <stdio.h>
#include <time.h>
#define VER_MAJOR 0
#define VER_MINOR 1
int usage(const char* msg)
{
if(msg != NULL) {
printf("error: %s\n", msg);
}
printf("gdplay %d.%d, a utility just for fun\n"
"syntax: gdplay [options]\n"
"\tsupported options:\n"
"\t-h show this help\n"
"\t-t print time\n", VER_MAJOR, VER_MINOR);
}
void gd_time()
{
time_t now;
struct tm* tminfo;
char buffer[80];
time(&now);
tminfo = localtime(&now);
strftime(buffer, sizeof(buffer), "%F %h %H:%M:%S %p %c", tminfo);
puts(buffer);
}
int main(int argc, const char* argv[])
{
printf("main received %d arguments\n", argc);
for(int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
if(argc <= 1)
return usage("missing arguments");
if(argv[1][0] == '-') {
switch(argv[1][1]) {
case 'h':
return usage(NULL);
case 't':
gd_time();
break;
}
}
return 0;
}