I want to execute user define function before main().
Is it possible to execute a function before main()
in c?
sum(int a, int b) { return (a+b); }
g_sum = sum(1, 5);
main(){
sum(5, 6);
printf("%d", g_sum);
}
I want to execute user define function before main().
Is it possible to execute a function before main()
in c?
sum(int a, int b) { return (a+b); }
g_sum = sum(1, 5);
main(){
sum(5, 6);
printf("%d", g_sum);
}
Is it possible to execute a function before main()
Yes it is possible if you are using gcc and g++ compilers then it can be done by using __attribute__((constructor))
Example:
#include <stdio.h>
void beforeMain (void) __attribute__((constructor));
void beforeMain (void)
{
printf ("\nThis is before main\n");
}
int main ()
{
printf ("\nThis is my main \n");
return 0;
}