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?
There's one error: &"Hello Stackoverflow"
does not have type char*
, 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.
The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type ofint
and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
(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.
You mean beside it won't run? main
is defined to take int, 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.
It's only valid under C99 and later if the implementation explicitly documents that main
may take 5 parameters (4 int
and 1 char *
) and return void
(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.