You are here: Home / Topics / How to call a function before main() in C programming?

How to call a function before main() in C programming?

Filed under: C Programming on 2022-07-16 16:39:34

To call a function before the main(), pragma startup directive should be used. E.g.-

#pragma startup fun

void fun()

{

printf("In fun\n");

}

main()

{

printf("In main\n");

}

The output of the above program will be

In fun

In main

This pragma directive, on the other hand, is compiler-dependent. This is not supported by gcc. As a result, it will ignore the startup directive and produce no error. But the output, in that case, will be

In main

About Author:
M
Manisha Agrawal     View Profile
Hey, this website is very nice for mcqs.