Friday 20 March 2015

Arduino Tips & Tricks: 

Where is the main() function in the arduino codes?

While writing a code  on AVR studio or keil uvision every code should have a main function. And during the days when i started programing my teacher told me that every c /c++ code should have a main() function and there should be one and only one main in any program. While writing big programs the programmers tend to write different parts of program in different tabs for better readability and understanding and sometimes  write #include<abc > in the tab where main function is written to include those files 
But in an arduino environment you never find  something like below
void main()
{

}
well does that mean it does't have one. It seems so but it is not actually like that as basics never go out of fashion. It is just another way of arduino to make things simple by hiding the void main. Actually what happens behind the scene is something like below
int main(void)
{
    init();

    setup();

    for (;;) {
        loop();
    }

    return 0;
}
so the setup() and loop() are actually part of main. And the loop function is actually a function under infinite for loop. This also explains why the setup()  runs only once and loop() is executed again and again

No comments:

Post a Comment