main函数的两个问题

来源:互联网 发布:域名被劫持怎么办 编辑:程序博客网 时间:2024/06/05 02:45

1、带参数的main函数的一种标准形式

     记住以后写不带参数的main函数这样写:  

int mai(void){     return 1;}
     带参数的main函数形式如下:

       int main(int argc, char *argv[]) ;argc 参数个数,包括可执行文件名,argv是指针数组,该数组的元素是指向字符串的字符指针,

argv[0]是指向可执行文件名字符串的字符指针,以此类推。

#include <iostream>

using namespace std;int main(int argc,char *argv[]){for(int i=0;i<argc;i++){cout<<argv[i]<<'\t';    }return 0;}

cmd-debug目录下 输入 cppTry  I  Love  My  Family  输出 cppTry  I  Love  My  Family


2、main 主函数执行完毕后,可能会再执行一段代码,说明如下

     先认识下一个函数: _onexit()函数

     The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

     关于 _onexit()函数,最重要的就是LIFO被注册的函数不能带参数

#include <iostream>#include <string>using namespace std;int fn1(){printf("f1.\n");return 0;}int fn2(){printf("f2.\n");return 0;}int fn3(){printf("f3.\n");return 0;}int fn4(){printf("f4.\n");return 0;}void main( void ){string str("zhanglin");_onexit( fn1 );_onexit( fn2 );_onexit( fn3 );_onexit( fn4 );printf( "This is executed first.\n" );}





    


原创粉丝点击