Tuesday, July 14, 2009

Is the main function in C language is user defined or predefined. Can any one explain.?

The contents of the main function are for sure user defined.


The signature of the same however is predefined


It can only be on of these two in C


main();


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





The return type of main is subject to a lot of arguments.


In C++, the return type has to be "int"


If I am not wrong, there is no such requirement in C, this mean you can get away with both int and void


I was once told that you should be able to get away with other types as well, but that I have never tried out for myself, so I cant vouch for it. I guess that may be compiler dependent.





So to conclude, these are allowed


int main()


void main()


int main(int argc, char *argv[])


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





Hope this helps

Is the main function in C language is user defined or predefined. Can any one explain.?
main () is just like any other function.


You can write it any way you like, and call it any time you want. There is absolutely nothing "predefined" about it in any way.


Those, who say, arguments are fixed are wrong to.


You can have


main() or main (int) or main (int,char**) or main (int, void*) or main (int,void**,float,double) - whatever you make it. And it can return anything you want too, or nothing at all.





The only special thing about main is that the progam loader, looks for a function with this name when you start a program, and calls it passing the number of command line arguments as the first parameter, and an array of their values as the next,


and when the program exists, the value main() returned is converted to int, and returned to the OS as the program's exit status.
Reply:usually predefined


although,a user change option is offered


for more help,i would reference:


http://www.techrepublic.com


free logon and search available
Reply:The main function is the entry point. The main function is the first USER written function when your program is executed. Some system code usually runs before the main function executes. I've had a program choke before the main function because I gave it the wrong flags during compile time which was proof that some system code actually ran before the main function.


No comments:

Post a Comment