c/c++ 的 main 前 main 後.

c/c++ 在 main 函數之前和之後發生的那些事。

 

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

void foo()
{
    printf("call back atexit, C can do it after main.\n");
}
int bar()
{
    printf("global variable initialized, C can do it before main.\n");
    atexit(foo);
    return 0;
}

int x = bar();

class CTest {
    public : 
        CTest() {printf("constructor, C++ can do it before main.\n");}
        ~CTest() {printf("destructor, C++ can do it after main.\n");}
};

CTest ct;

int main(void) 
{ 
printf("hello world!\n"); 
return 0;
} 

 

posted on 2013-03-04 21:50  mhgu  阅读(181)  评论(0编辑  收藏  举报