int main(void) vs void main(void)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

int main(void) vs void main(void)

1,045 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Tue May 10 22:50:24 MST 2011
CodeRedSupport,

I know that the underlying reason of int main(void) is due to GCC historical reasons for operating systems,  but we can't run a true operating system on these MCUs anyways.  Also returning from main() is a no-no.

So why do we have int main(void) in LPCXpresso? A return value is not necessary as the catch loop does not use it.

Wouldn't it make more sense to have void main(void)?  It would reinforce the fact that we do not return.
0 Kudos
Reply
5 Replies

1,020 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 11 18:33:26 MST 2011
There is quite a discussion on another forum about this. Some quotes:


Quote:
You must use '[B]void[/B] [B]main[/B]()' as any sane person would.



That would probably not apply to me.;)


Quote:
As such the "[B]void[/B] [B]main[/B]([B]void[/B])" used by CV (and others I think) is far more sensible for an MCU.




Quote:
As far as I have always understood,  you can use int [B]main[/B]() or [B]void[/B] [B]main[/B]() as appropriate.

Without an operating system,  [B]void[/B] seems 'appropriate' to me.    So I would instinctively use [B]void[/B] [B]main[/B]().



As you said, it basically makes no difference, but the latter makes the most sense.

Larry
0 Kudos
Reply

1,020 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 11 09:19:04 MST 2011

Quote: CodeRedSupport
To be honest, in the testing I have done - it basically makes no difference. Feel free to post an example where it does if you have one.



I was only exploring the "why",  I have no examples, yet.;)


Quote: CodeRedSupport

...if you are using the standard startup file, as this actually contains a while(1) loop after the call to main()/__main() anyway.



That was the "catch loop" I mentioned above.  It's a good place to drop our code into.  The realm of "no main's land".:)
0 Kudos
Reply

1,020 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed May 11 08:41:43 MST 2011

Quote: larryvc

So why do we have int main(void) in LPCXpresso? A return value is not necessary as the catch loop does not use it.



To be honest, in the testing I have done - it basically makes no difference. Feel free to post an example where it does if you have one.

One thing to note is that although the default code provided in main.c by the project wizard includes an infinite while(1) loop - you can actually remove this if you are using the standard startup file, as this actually contains a while(1) loop after the call to main()/__main() anyway.

Having the while(1) in main() just makes the loop more obvious, and as it increments a counter, makes it easier to see that the loop is looping.

Regards,
CodeRedSupport
0 Kudos
Reply

1,020 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 11 08:15:59 MST 2011

Quote: atomicdog
I believe int main() is required by the C standard, at least for a non-freestanding environment.



Hi John,

Yes that was what I was trying to say above, but we are mostly working on freestanding environments while using LPCXpresso and I don't have a CodeRed version but I imagine there as well.
0 Kudos
Reply

1,020 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Tue May 10 23:56:15 MST 2011
I believe int main() is required by the C standard, at least for a non-freestanding environment.
0 Kudos
Reply