Monday, April 6, 2009

A portable main()

Question:
Is it possible to write a portable main() function for a C program in all hosted environments?
The C++ standard says that a conforming implementation shall support the two forms of main (int main() and int main(int argc, char *argv[]).
The C standard does not put any such requirements. So as far as I can see, it's perfectly possible for a conforming implementation to not support int main(void).

So, strictly speaking, for C programs, is int main(void) any better than void main(void)?
I said 'strictly speaking' because I'm aware that a much larger number of platforms might support int main() than void main(). But my question is about absolute portability - i.e. one that works on all conforming hosted environments.

Answer1:
In the C standard, The comments about main is
It can be
1) int main( void )
2) int main( int argc, char **argv )
3) any implementation defined manner.

The third point clearly specifies that the compiler can well support void main( void )

But almost all the Windows compilers also support int main( void/2 arg/3 arg ) format. The 3 arg format can't be considered as a standard one, it is classified as common extension.

So I think, int main(void) is portable in any case, and int main(2 arg ) is portable, if the environment support command line arguments.

Answer2:
In the C standard, The comments about main is
It can be
1) int main( void )
2) int main( int argc, char **argv )
3) any implementation defined manner.

The third point clearly specifies that the compiler can well support void main( void )

But almost all the Windows compilers also support int main( void/2 arg/3 arg ) format. The 3 arg format can't be considered as a standard one, it is classified as common extension.

So I think, int main(void) is portable in any case, and int main(2 arg ) is portable, if the environment support command line arguments.


Answer2 Qestions Answer1:
So I think, int main(void) is portable in any case
How is that? The standard says any one of the listed three forms. So it is okay for a conforming implementation to not support the first two. This is not possible for C++ since the standard says a conforming implementation must support the first two.


Do You Have Any Answers? Comment Here:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.