return type of main

Discussion in 'Programmer's Corner' started by jayanthyk192, Aug 12, 2010.

  1. jayanthyk192

    Thread Starter Member

    Jan 23, 2010

    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?
  2. Papabravo


    Feb 24, 2006
    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
    Code ( (Unknown Language)):
    2. void main(void)
    3. {
    4.   ...Initialization code
    5.   while(1)
    6.   {
    7.     ...background loop
    8.   }
    9.   ...dead code -- execution never reaches this point
    10. } // 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??
  3. debjit625

    Well-Known Member

    Apr 17, 2010
    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.
  4. retched

    AAC Fanatic!

    Dec 5, 2009
    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. ;)
  5. Papabravo


    Feb 24, 2006
    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.