What structure loop() mean? Does it work like main() in C? What other loop func?

I am trying to understand structure loop() in Arduino. How does it work. If it works like other loop functions what about using them then? Can I compare loop() to function main() in C? But it does not work in repetitive way like loop function it rather works once only.

I would appreciate for any help to explain me all of this. Thank you in advance.

I am trying to understand structure loop() in Arduino.

loop() is not a structure. It is a function. It is called over and over again by the main() function (that you don’t see/write).

If it works like other loop functions what about using them then?

What about using them?

Can I compare loop() to function main() in C?

No.

This is the main() that is written for you in the Arduino core. Make more sense now?

int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
        
    return 0;
}

Thank you for your help. I can understand this better now. So what we put into loop() it really works in infinitive loops for(;;) or while(1). Can we use our defined functions and they must be always inserted in this loop()? Sometimes we need to write the code outside the loop so this sort of code will land in setup() function then?

If you want it to happen once at startup, put it in setup. If you want it in the repeated code, put it in loop. They are normal functions like any other in C++. Call anything you want from either of them.

Hello and welcome,

If you know what you are doing, you can redefine main() yourself. For example, this is a perfectly valid sketch:

int main()
{
  Serial.begin( 9600 );
  Serial.println( "setup" );

  while ( true )
  {
    Serial.println( "loop" );
  }

  return 0;
}

Of course in this example, some things will not work ( millis() for example ) because I didn't add a call to init().

The code in Several Things at a Time and in Planning and Implementing a Program are more extensive examples of Arduino programs with several functions. They may help to illustrate the concepts.

...R

Thank you for so useful informations