Main() arduino function change

Hi guys i am a newbie here. I just learned about the main() function in program which calls setup() and then infinite loop for loop() function. I just wanted to know if i can tinker with this main function to change the flow like call loop() first or call loop() for a certain number of loop instead of infinite one. What is the process to do the same.

Although this may be technically wrong, i want to tinker with the same to i try to learn through observing the behaviour of program flow at outcome.

You can define a main function in your sketch. This function will override the one in the core. That is the easiest way to experiment with the function.

Keep in mind that the default main does some initialization of the board that we take for granted. If you leave that code out of your custom main function, you might find that the board is not working as you expect.

This is most noticeable with the boards that have native USB capability (e.g., Leonardo). The code in the default main actually creates the CDC serial port. So if you upload a sketch like this to a native USB board:

int main() {
  while(true) {}
  return 0;
}

You will find that the serial port of the board has disappeared and you can no longer upload to it because the serial port is needed by the IDE to send the reset signal that puts the board in bootloader mode at the start of the upload. It is actually easy enough to manually put the board into bootloader mode to restore it to normal working condition, but perhaps an unpleasant experience for those who were not expecting it.

This is only one example of the possibly unexpected result of playing with main. In some cases, you might actually find this is useful in case you wanted to use custom USB code or even do without it completely in an application where it is not needed or even harmful (note how much less memory that sketch uses than the normal bare minimum sketch!).

1 Like

Then what is the default main() code?

And just out of curiousity:
Why is it int main() instead of void main()?

Thanks for the quick and through solution. I just wanted to know if i keep the usb portion of the code intact and just tinker with setup() Nd for(;:wink: loop() sections of the code will still the usb functionality be disabled?

And can i also do this for esp8266?

It is defined by the core of the board you are compiling for, so it might be different from one board to another.

Here you can see the default main function for the boards of the "Arduino AVR Boards" platform:

Here you can see it for the "Arduino SAMD Boards (32-bits ARM Cortex-M0+)" boards platform:

Because in a PC application it is useful to have an exit code for your C++ program:

https://cplusplus.com/doc/tutorial/program_structure/

Probably not so much in the world of embedded systems, but I guess there is a blurred line these days.

It should be fine. Give it a try and ask for help on the forum if you run into trouble. There isn't much you can do in code that will permanently harm your board.

That is a good question. I don't have much experience with the ESP8266. Some of the other forum members do, so I'm sure one of them can provide a better answer.

From a quick glance, It looks like it is not so straightforward as in the cores I am more familiar with.

There is some interesting information in this comment:

You might also take a look at the code in that file.

1 Like

Just remember to keep a copy of any package file that you decide to change.

1 Like

Tinkering is fine but really your ask can very likely be achieved within the existing setup and loop

2 Likes

Required by the C++ standard.

1 Like

as other said, this is by language definition. Here you can see what it looks like

A program shall contain a global function named main, which is the designated start of the program in hosted environment. It shall have one of the following forms:

  • int main () { body } (1)

  • int main ( int argc, char *argv[]) { body } (2)

  • another implementation-defined form, with int as return type (3)

1 Like

Then you already know what you need to know for basic, intermediate and 99% of advanced level Arduino projects.

There are much more important and interesting things to learn about Arduino, circuits and coding, than this.

This would not work in most sketches because the Arduino's internal hardware or the external hardware of your circuit would not be prepared and ready for the code in loop() to run.

This can easily be achieved without changes to main().

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.