This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

main function ?

why in Keil compiler we define the main as int rather that void ?

int main (void)

rather than ....

void main (void) ,like in C51

  • This is because the RealView compiler is strict ANSI (even when a return type in an embedded system makes no sense on embedded systems).

  • HI,,
    I agree that is a strict ANSI.
    But when devoloping code for 8051 we dont use int main.
    why?
    what is the deifference?

  • Why is all compilers a little bit different?

    Maybe because the ARM processor has more RAM and more flash memory?
    Maybe because the ARM compiler has a different development history?
    Maybe because someone just took a decision?

    There are way more important differences when moving from one embedded environment to another than if main() is declared void or int :)

  • Please if u know the answer say it,
    rather than waisting ur time saying rubish......

  • "But when devoloping code for 8051 we dont use int main.
    why?"

    Because C51 isn't strict ANSI/ISO.

    "what is the deifference?"

    It should be obvious by now; one is strict ANSI/ISO and the other is not.

  • You are approaching this from the wrong direction!

    The ANSI standard states that main() returns an int result - so you should rather be asking why C51 violates that requirement.

    The 8051 is a small, simple processor and is very likely to be used in systems that are very tightly constrained on code and memory space - therefore anything that gives any unnecessary overhead needs to be removed.
    Also, C51 has a very long history, dating back to times when these constraints were even more pressing - hence it was doubly important to remove any unnecessary bloat.

    None of this is true for the ARM itself or the Realview compiler - so there is no real reason for it to flout the requirements of the ANSI specification.

  • "The ANSI standard states that main() returns an int result"

    Actually, I've just checked in ISO/IEC 9899:1990 - and that's not strictly true!

    It is true for a Hosted environment but, for a Freestanding environment, the specification says:

    "the name and type of the function called at program startup are implementation-defined. There are otherwise no reserved external identifiers."

    So C51 is fully ANSI-compliant for a Freestanding environment.

    And Keil's ANSI Compliance page says,

    "Compilers from Keil are conforming freestanding implementations"
    http://www.keil.com/product/isoansi.asp

    In other words, Keil needn't even have chosen "main" as the name for the program's main function - but they did.

    Again, an 8051 is far more likely to be used in a Freestanding environment than an ARM - and this would have been especially true when C51 was first developed.

  • And more importantly - the C51 was developed long before the current C standard.

    Changing the return value of main() would have required a compilation option, since a number of users would have been unhappy if they had to modify any source lines to be able to compile newer application. Some companies have very, very, very strict policies about changing any source code lines, even to avoid a compilation warning. Such a tiny source-code change could result in a requirement to perform several months of recertification of the application.