return type of main

Thread Starter

jayanthyk192

Joined Jan 23, 2010
80
hi,

according to the standard the signature of main() is either int main(void) or int main(int argc, char **argv).

However, it works even if it is given as void main().


When does this make a difference? I mean, in an environment where the process return a value to the OS,the integer returned serves as an error checking mechanism.

Can you please in terms of an embedded system, as to where the value is returned, if given as int main() and, is giving void main() a mistake?
 

Papabravo

Joined Feb 24, 2006
21,225
In an embedded systems it makes absolutely no difference since having main return at all is an error because there is no OS for main() to return to. Most embedded C startup files don't even stack a return address they just jump to main() If they do actually call main() then they will execute a jump to "current address" if main ever returns effectively crating an infinite loop. Of course if interrupts are still enabled they will be serviced but the background program is toast.

Typical embedded main() function
Rich (BB code):
void main(void)
{
  ...Initialization code
  while(1)
  {
    ...background loop
  }
  ...dead code -- execution never reaches this point
} // Implied return is never executed
Don't you guys from India ever look at the compiler output to see what the heck it is doing to you??
 

debjit625

Joined Apr 17, 2010
790
Don't you guys from India ever look at the compiler output to see what the heck it is doing to you??
Yes we look ,but its not about India or USA every where their are guys who dont know something and wants to learn and its the case.
 

retched

Joined Dec 5, 2009
5,207
Actually it is geographic. When I was visiting India, building a prototype for a company, I COMPLETELY FORGOT to look at complier output. And it wasn't until the plane landed back in the USA did I look at the output and find the problem!

just kidding. ;)
 

Papabravo

Joined Feb 24, 2006
21,225
Actually it is geographic. When I was visiting India, building a prototype for a company, I COMPLETELY FORGOT to look at complier output. And it wasn't until the plane landed back in the USA did I look at the output and find the problem!

just kidding. ;)
So was I actually. It was only a mild jab and I apologize if you took offense. The real point is that answers can be right in front of us if only we know where to look, and compiler writers DO in fact make mistakes that are difficult to ferret out.
 
Top