Does this code follow C standards (e.g. C89, C99, C10x)?
void
main(int a,int b, int c, int d,char *msg){
if(d==1){
printf("%s\n",msg);
}else{
main(1,2,3,1,&"Hello Stackoverflow");
}
}
If not, why?
Does this code follow C standards (e.g. C89, C99, C10x)?
void
main(int a,int b, int c, int d,char *msg){
if(d==1){
printf("%s\n",msg);
}else{
main(1,2,3,1,&"Hello Stackoverflow");
}
}
If not, why?
It's only valid under C99 and later if the implementation explicitly documents that
main
may take 5 parameters (4int
and 1char *
) and returnvoid
(that's the "or in some other implementation-defined manner" clause that larsmans referenced in his now-un-deleted answer, and I don't think that clause was present in C89).Otherwise the behavior is undefined, meaning the compiler may or may not choke on it.
You mean beside it won't run?
main
is defined to takeint, char**
as arguments.Depending on the compiler, this either will fail to start up as the run-time can't find
main(int, char**)
, or on older compilers it'll just crash because it piddles on the stack.There's one error:
&"Hello Stackoverflow"
does not have typechar*
, so you shouldn't pass that to a function expecting that type.Apart from that, this program is allowed by the Standard as an implementation-specific extension, but a compiler has the freedom to decline it.
(2011 Standard, latest draft section 5.1.2.2.1, emphasis added.)
There is no ban on recursive calls to
main
in the C Standard. This is a difference with C++, which does outlaw that.