main函数能调用吗?我想,它既然也是个函数,那它就应该有普通函数的特性,只是因为名字是main,所以被编译器作为代码段的入口而已。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
if (argc > 10) exit(0);
printf("%d\n", argc);
main(argc + 1, argv);
return 0;
}
该程序的运行结果:
1
2
3
4
5
6
7
8
9
10
还可以做别的更无聊的事情,比如,计算1到100的整数的和:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
if (argc == 100)
{
return argc;
}
else if (argc == 1)
{
printf("%d\n", 1 + main(argc + 1, argv));
}
else
{
return argc + main(argc + 1, argv);
}
return 0;
}
输出结果很明显:
5050