Created
November 8, 2024 15:55
-
-
Save denisdemaisbr/84d9052a322881ed34d1b7a10615dc7c to your computer and use it in GitHub Desktop.
a c example of constructor/destructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://www.geeksforgeeks.org/functions-that-are-executed-before-and-after-main-in-c/ | |
*/ | |
#include<stdio.h> | |
/* Apply the constructor attribute to myStartupFun() so that it | |
is executed before main() */ | |
void myStartupFun (void) __attribute__ ((constructor)); | |
/* Apply the destructor attribute to myCleanupFun() so that it | |
is executed after main() */ | |
void myCleanupFun (void) __attribute__ ((destructor)); | |
/* implementation of myStartupFun */ | |
void myStartupFun (void) | |
{ | |
printf ("startup code before main()\n"); | |
} | |
/* implementation of myCleanupFun */ | |
void myCleanupFun (void) | |
{ | |
printf ("cleanup code after main()\n"); | |
} | |
int main (void) | |
{ | |
printf ("hello\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment