main( ) function

I want to know something regarding main function. For an application, the syntax for main function is something like:
void main(void)
{
// ...
// code
// ...
}
but if we write as:

int main(void)
{
// ...
// code
// ...
return(0);
}

Here, who will handle the return value from main function. Normally embedded application is an infinite loop, so there wont be any chance of returning from main. But still it it terminates, then who will there to handle return value from main.

Parents
  • "Here, who will handle the return value from main function."

    Nothing will handle it - but that in itself is no problem. It is perfectly acceptable in 'C' for a function's return value to be ignored.

    "Normally embedded application is an infinite loop, so there wont be any chance of returning from main."

    Exectly - so it really makes no difference what return type you define for main() - since main() should never return!

    "But still it it terminates"

    No - that's the point: you need to ensure that main() never does terminate!

    (In fact, I think someone mentioned here a while back that C51 automatically adds an inifinite loop to catch any case where main() does exit...?)

Reply
  • "Here, who will handle the return value from main function."

    Nothing will handle it - but that in itself is no problem. It is perfectly acceptable in 'C' for a function's return value to be ignored.

    "Normally embedded application is an infinite loop, so there wont be any chance of returning from main."

    Exectly - so it really makes no difference what return type you define for main() - since main() should never return!

    "But still it it terminates"

    No - that's the point: you need to ensure that main() never does terminate!

    (In fact, I think someone mentioned here a while back that C51 automatically adds an inifinite loop to catch any case where main() does exit...?)

Children
More questions in this forum