- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Functions that are executed before and after main() in C
Here we will see how to write a code where two functions are present, and one function will be executed before the main function, and another function will be executed after the main function. These features are used to do some startup task before executing the main, and some cleanup task after executing main.
To do this task we have to put attribute for these two functions. When the attribute is constructor attribute, then it will be executed before main(), and when the attribute is destructor type, then it will be executed after main().
Example Code
#include<stdio.h> void before_main() __attribute__((constructor)); void after_main() __attribute__((destructor)); void before_main() { printf("This is executed before main.
"); } void after_main() { printf("This is executed after main."); } main() { printf("Inside main
"); }
Output
This is executed before main. Inside main This is executed after main.
- Related Articles
- Before and After Puzzle in C++
- Why do some Python functions have underscores "__" before and after the function name?
- Fire trigger after the DELETE operation is executed in MySQL
- The ::before and ::after Pseudo-element in CSS
- When and where static blocks are executed in java?
- What to Eat Before and After your Workout?
- Difference between void main and int main in C/C++
- Difference between “int main()” and “int main(void)” in C/C++?
- Functions that can’t be overloaded in C++
- Functions that cannot be overloaded in C++
- How to preview an image before and after upload in HTML and JavaScript?
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- What is food? What are its main functions, and is there any source of food except plants and animal?
- Differentiate between int main and int main(void) function in C
- Does Process before output and process after input initiate commits in ABAP?

Advertisements