
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
attribute((constructor)) and attribute((destructor)) syntaxes in C in tutorials point ?
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 main function. These features are used to do some startup task before executing main, and some clean up 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().
We are using GCC functions. The function is __attribute__(). In this case we are using two different options. The Constructor and the Destructor with the __attribute__() function. The syntax __attribute__((constructor)) is used to execute a function when the program starts. and the syntax __attribute__((destructor)) is used to execute the function when main() function is completed. Please go through the example to get better idea.
Example
#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
- Order of Constructor/ Destructor Call in C++
- Difference Between Constructor and Destructor
- Selects all elements with alt attribute containing the word "Tutorials" with CSS
- Private Destructor in C++
- Virtual Destructor in C++
- Write a program to print ‘Tutorials Point’ without using a semicolon in C
- Attribute Selectors in CSS
- Get and Set the stack size of thread attribute in C
- Pure virtual destructor in C++
- Composition attribute in HTML5 Canvas?
- Write a C program to print “ Tutorials Point ” without using a semicolon
- What does the [Flags] Enum Attribute mean in C#?
- HTML5 preload attribute
- HTML pattern Attribute
- HTML novalidate Attribute
