If the main() takes 3 arguments i.e. int argc,char * argv[],char *env[] and SINCE C DOES NOT SUPPORT FUNCTION OVERLOADING ,y does the c compiler does not give error for simply

void main() //that is no arguments at all

OR

void main(int argc,char *argv[]) //2 arguments

main is a special function because it's the program's entry point. Thus, there's a certain measure of compiler magic involved. During compilation the compiler will determine which version of main to call based on your definition.

ohk..thnx :)

If the main() takes 3 arguments i.e. int argc,char * argv[],char *env[] and SINCE C DOES NOT SUPPORT FUNCTION OVERLOADING ,y does the c compiler does not give error for simply

void main() //that is no arguments at all

OR

void main(int argc,char *argv[]) //2 arguments

There are two things wrong with your question. First, main() returns int. Always and for ever. void main() is not legal C (under a hosted environment). Your options are (under C99):

int main(void);
int main(int argc, char *argv[]);
int main(int argc, char *argv[], char *envp[]);

Also, int main() isn't a prototype, so it can refer to main no matter what parameters it takes. void main() does not imply no parameters at all, but merely that the number and type of parameters are not known.

As for the actual question, the answer is that there's no overloading going on for the simple reason that there's no conflict. A C program can have at most one definition of main(). The compiler will see which version you used and generate the appropriate code to initialize whatever parameters your program expects. This isn't overloading or anything like it.

>void main() is not legal C (under a hosted environment).
Incorrect. Since you seem to be a standard reader, look it up and you'll see "or in some other implementation-defined manner". This is an escape clause that allows implementations to support extended functionality, such as not requiring a return value (ie. void main) or additional parameters such as the common envp third parameter.

>Your options are (under C99):
>int main(int argc, char *argv[], char *envp[]);

Incorrect. C99 only defines explicitly a zero parameter version:

int main(void) { /* ... */ }

And a two parameter version:

int main(int argc, char *argv[]) { /* ... */ }

Your three parameter version falls under the escape clause, though it is mentioned as a common extension in annex J section 5 of C99.

>void main() is not legal C (under a hosted environment).
Incorrect. Since you seem to be a standard reader, look it up and you'll see "or in some other implementation-defined manner". This is an escape clause that allows implementations to support extended functionality, such as not requiring a return value (ie. void main) or additional parameters such as the common envp third parameter.

Conceded. I should have said "not standard" rather than "not legal", as the standard allows, but does not dictate, this form.

Your three parameter version falls under the escape clause, though it is mentioned as a common extension in annex J section 5 of C99.

My careless mistake. In fact, I was trying to say (correctly) that the three parameter version was nonstandard, when I discovered it in the actual standard. I revised my post without paying attention to the context of its mention. Thank you for the correction.

thnx a lot 2 both :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.