Where is Main Function


There are mentioned in the Getting Started, hello world is usually the first program we learned. Remember the c language hello world ?

// hello world
#include <stdio.h>

int main()
{
    printf("hello world!\n");
    return 1;
} 

Do you find that there is not main fucntion, replaced by setup and loop. This is troblesom to some beginners, you must want to know who stole my main function?
In fact, Arduino has main, but it’s hiden. You can fint it here: \arduino-1.0.5\hardware\arduino\cores\arduino\main.cpp, as folowing:

#include <Arduino.h>

int main(void)
{
    init();
#if defined(USBCON)
    USBDevice.attach();
#endif
    setup();
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
    return 0;
}

Yep, You can find that Arduino did had a main. In the front of main, there is some initialization, and then setup, followed by a cycle of death, loop. So now you know why you can’t fine main, and why is setup run once, and loop run over and over again.


Resources

下载PDF文档 download pdf file

Copyright (c) 2008-2016 Seeed Development Limited (www.seeedstudio.com / www.seeed.cc)
This static html page was created from http://www.seeedstudio.com/wiki