Open In App
Related Articles

__attribute__((constructor)) and __attribute__((destructor)) syntaxes in C

Improve Article
Improve
Save Article
Save
Like Article
Like

Write two functions in C using GCC compiler, one of which executes before main function and other executes after the main function.

GCC specific syntaxes :

1. __attribute__((constructor)) syntax : This particular GCC syntax, when used with a function, executes the same function at the startup of the program, i.e before main() function.

2. __attribute__((destructor)) syntax : This particular GCC syntax, when used with a function, executes the same function just before the program terminates through _exit, i.e after main() function.

Explanation :
The way constructors and destructors work is that the shared object file contains special sections (.ctors and .dtors on ELF) which contain references to the functions marked with the constructor and destructor attributes, respectively. When the library is loaded/unloaded, the dynamic loader program checks whether such sections exist, and if so, calls the functions referenced therein.

Few points regarding these are worth noting :
1. __attribute__((constructor)) runs when a shared library is loaded, typically during program startup.
2. __attribute__((destructor)) runs when the shared library is unloaded, typically at program exit.
3. The two parentheses are presumably to distinguish them from function calls.
4. __attribute__ is a GCC specific syntax;not a function or a macro.

Driver code :




// C program to demonstrate working of
// __attribute__((constructor)) and
// __attribute__((destructor))
#include<stdio.h>
  
// Assigning functions to be executed before and
// after main()
void __attribute__((constructor)) calledFirst();
void __attribute__((destructor)) calledLast();
  
void main()
{
    printf("\nI am in main");
}
  
// This function is assigned to execute before
// main using __attribute__((constructor))
void calledFirst()
{
    printf("\nI am called first");
}
  
// This function is assigned to execute after
// main using __attribute__((destructor))
void calledLast()
{
    printf("\nI am called last");
}


Output:

I am called first
I am in main
I am called last

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Jun, 2017
Like Article
Save Article
Previous
Next
Similar Reads