avoid "void loop()"

hi,i'm new with Arduino I saw some examples,all of them use the function : "loop" am I forced to do it? isn't there somthing like "main()" function (without any loop) ? thanks in advance

I saw some examples,all of them use the function : "loop" am I forced to do it?

Yes.

isn't there somthing like "main()" function (without any loop) ?

A sketch does not have a main function because the compiler adds a main function that loops like:

int main()
{
   init();
   setup(); // Your setup function

   for(;;)
   {
      loop(); // Your loop function
   }
}

If you try to add a main() function, the compiler will complain that you have duplicated the existing main() function.

ok,I see
what should I do if I want to run the program only once (no loop)?
I don’t want to use an endless loop

ok,I see what should I do if I want to run the program only once (no loop)? I don't want to use an endless loop

Just place all your code into the setup function and leave the loop function empty. Your code will execute just once and then the processor will just spin in a do nothing loop.

Lefty

ah ok ;D thanks a lot

Actually, you can indeed write your own main function. All that is required is that you call init() before any other code.

I usually do this myself, since I prefer the more standard format of main(). I have been doing this with 0021, and I may have tested it in 0022 last night. I believe the reason it works is that main() is compiled into a library, and is thus overwritable.

Remember to return 0; at the end of main().

I would upload an example program, but I am unfortunately without a pc for a couple days, and am posting from my iPod.

Remember to return 0; at the end of main().

Why?

Remember to return 0; at the end of main().

Why?

because of the signature ...

int main()

because of the signature ...

So, return -14; would be incorrect?

What happens if main does not include a return statement? A compiler warning is generated. The folks at Arduino-ville have decided that we don't need to know that. The code will still run, but what happens when main ends is undefined, because no value gets pushed onto the stack for the OS to pop.

Oh, wait, there's no OS to pop a value. So, what really happens when main does not include a return statement?

So, return -14; would be incorrect?

I once read that 42 was the ultimate return value :slight_smile:

Oh, wait, there’s no OS to pop a value. So, what really happens when main does not include a return statement?

In a windows or other OS the return value of main is returned to the calling shell e.g. cmd.exe

On an Arduino normally the end of main isn’t reached so main() never returns. So it is an fascinating question to quote Spock…


update
As init, setup, and loop adds things to the stack the caller of main might pop the value of the first declared variable from the stack? Or one gets an stack underflow? Fascinating …

What happens when main returns? Easy to tell from a disassembly. Here’s the call to main, coming as it does from gcrt1.S in avr-libc.

     call      0x2a3e      ; 0x2a3e <main>
     jmp      0x301a      ; 0x301a <_exit>

Returning will result in a jump to _exit. What’s that I hear nobody asking?

0000301a <_exit>:
    cli

0000301c <__stop_program>:
    rjmp      .-2            ; 0x301c <__stop_program>

Disable interrupts and go into an infinite nothing loop. So there you go.

Thanks Andy,

fascinating .... but _exit cannot be called directly... , lets check exit() ... yes that works

void setup()
{
  Serial.begin(115200);
  Serial.print("I quit");
  exit(0);
}

void loop()
{
  Serial.print(".");
  delay(100);
}

Thanks! I was wondering about that.

As init, setup, and loop adds things to the stack the caller of main might pop the value of the first declared variable from the stack? Or one gets an stack underflow? Fascinating ...

Once in loop, there are four bytes "orphaned". This recovers the four bytes...

#define FOREVER { SP = RAMEND; } while ( 1 )

void loop( void )
{
  FOREVER
  {
    // put your code here
  }
  // If execution reaches this point, something bad is going to happen!
  Serial.println( "Yikes!" );
}

Use at your own risk. I do.