Hello to all again. This is your friend Vineet presenting to you an EPRA Blog continuing from where we left i.e first blog entry of the basic “Hello-World” example. In that example we used constructor and destructor functions. You may argue that the use of constructor and destructor is associated with OOPs. But what you fail to consider is compiler extensions. In our case on Linux based systems the toolchain we used to build code was GNU toolchain consisting of tools such as GCC compiler. __attibute__ is one such extension which defines  attributes which are implemented by GCC alone and not part of ANSI C or MISRA C or any Standard C. For other toolchains to get similar behavior we generally use #pragma directives. But GCC way of handling these extensions is superior when compared to other toolchains as #pragma’s if they are not supported even then they would be compiled instead of generating an error although the functionality which we wanted to have is ignored. It gives us an illusion that the code is portable on a different compiler whereas it is not.

For those who do not know the constructor and destructor functions are called before and after main()and independent of main. CRT or C Run Time which is part of the toolchain executes the hello and bye functions independent of main. for ex:–

#include <stdio.h>

void hello() __attribute__((constructor));
void bye() __attribute__((destructor));

int main (void)
{
printf (“n Inside main ()”);
}

void hello (void)
{
printf (“n I run first ()”);
}

void end (void)
{
printf (“n I run last ()n”);
}

Build the code on a linux system in a terminal with gcc -o code code.c after saving the code as code.c and run the output with ./code to get a better understanding of how CRT works.