The OP wrote:
I chanced to hit upon a very weird piece of code snippet and i
cannot account for its
functioning.
Ron Natalie wrote:
The language only requires the compiler properly support
int main() and int main(int argc, char* argv[])
Other signatures (as long as they return int) are up to the
language to deal with. Most likely you are just violating
the definitions and these end up not being resolved at link time.
I think there is more going on here then you gave the OP credit for,
but I also think it may be compiler-specific.
Here's the OP's code, edited to eliminate the C-specific header file
#includes:
int main (int a, int b, int c, int d, int e)
{
return (a + b + c + d + e);
}
When I compile and link this as a C (not C++) with all diagnostics
turned on, gcc version 3.3 gives me the expected warnings:
C:\djgpp\mystuff>gcc -W -Wall -pedantic -o mainarg.exe mainarg.c
mainarg.c:3: warning: second argument of `main' should be `char **'
mainarg.c:3: warning: third argument of `main' should probably be
`char **'
mainarg.c:3: warning: `main' takes only zero or two arguments
(Despite what you said about the definitions not being resolved at
link time, gcc will successfully link the program and generate an
executable. The return value of the program upon execution is
presumably garbage.)
But if I compile and link the exact same program as a C++ program,
again with all diagnostics turned on, gcc version 3.3 generates no
diagnostic:
C:\djgpp\mystuff>gcc -W -Wall -pedantic -o mainarg.exe mainarg.cpp
In other words, the compiler is issuing a diagnostic if it's a C
program, but not if it's a C++ program. I would have expected the
compiler to generate the same diagnostic in both cases.
(FWIW, Comeau's online compiler compiled the program both as a C
program and as a C++ program without generating a diagnostic in either
case.)
I assume this is compiler-specific - i.e., a bug (or "feature") in
gcc, not a C++ language issue. But before I go complaining to the gcc
mailing list, can anyone think of a reason why, as a C program, this
should get a diagnostic, but not as a C++ program?
Best regards,
Tom