在main函数执行之前和执行之后执行的方法

//

//  before&after.c

//  Created by labuser on 10/30/11.

//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.

//

 

#include <stdio.h>

 

void before() __attribute__((constructor));

void after() __attribute__((destructor));

 

void before() {

    //printf("This is function %s\n", __func__);

    fprintf(stdout, "In %s %d\n", __func__, __LINE__);

}

void after() {

    //printf("This is function %s\n", __func__);

    fprintf(stdout, "In %s %d\n", __func__, __LINE__);

}

 

int main(int argc, char* argv[]) {

    atexit(after);

    printf("This is function %s\n", __func__);

    return 0;

}

原文地址:https://www.cnblogs.com/shenfei2031/p/2229326.html