The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or click here to continue anyway

What happens before main() function is executed in C and why is it important? | My MicroController . com

In embedded applications, since we are working very close to the hardware and since we are typically working with limited space, strict power requirements, and exact timing restrictions, it is very important to know exactly what happens when we run a program from the beginning to the end. Most developers (especially those who come from the non-embedded development world) usually assume that running a C program starts from the main() function. This is not true.

There are a lot of steps that take place before main() is executed and understanding those steps will help a lot in gaining a better understanding of how the system works and ultimately will be extremely helpful in some debug situations. For example, in embedded systems it is possible to have your program crash before even reaching main(). Understanding what happens before main() will make you better at debugging these type of crashes.

The best way to learn is to run a very simple program (eg. a program with a main() function that simply returns zero) ideally on a microcontroller and use gdb to step through the code. While what happens before main(), is architecture and platform dependent, there are some generic steps that usually take place. Here they are in no particular order:

Note that some microcontrollers may require a start.c or cstart file that has the initialization code (whether this file is manually created or auto generated).

Now that we know what happens before main(), we might wonder if there’s a way we can control what happens before main, or if there is a way we can write our own version of what happens before main(). In C, the answer is largely no. Whatever happens before main() is largely dependent on your architecture and compiler. However this is in fact possible in C++.

One way you can do this in C++ is by declaring a global class object. Since global variables are initialized before main(), the constructor of the class you have initialized as global will run before main(). You can therefore place the code you want to run before main() in such a constructor.

A topic that is related to this is the boot loader, but I will save that for another post.

If you enjoyed reading this post, please support a new project that I have started called “UnCrowdMe – An app that tells you how busy your gym is” by subscribing to my YouTube channel here:https://www.youtube.com/channel/UCDDmUcthozizPiHcJ1adIAw

I upload weekly videos on on this channel and talk about the technical and non-technical challenges associated with this project along with tutorials and how-to’s.

Also, if you’re a starter but struggle with finishing things, get my kindle book:How to become a finisher

You can find me on LinkedIn:https://www.linkedin.com/in/arashf/

Please follow and like us:

Continue reading on mymicrocontroller.com