I am sure; you guys are quite familiar
with C programming, especially, while it comes to write the first line of your
main function. In my class, I have seen many of my students are writing main
function with many different forms.
Some portion of students write
like main(), some write main(void), again some of the students
write void main(void) and int main(void). Now the question is
which one is right?
All are right. Generally you
write your C programs with Turbo C editor, or with any other, and generally
they do not complain if you write any of them. But still, there is a factor of
theoretical perfection. Trivial programming exercises may not be affected with
whatever way you follow to declare main
function, but in critical cases, your decision may make differences. This is
why you should know which one is theoretically perfect.
As we always know that main is a function which gets called at
the beginning of execution of any program. So like other functions, main must have a return type and it
must expect arguments. If you are not calling your program (or your main likewise) from the command prompt,
your main function should not bother
for any arguments, so actually you should call your main with void argument,
like main(void).
Now you may raise a point that main() itself suggest that no arguments
are passed, so what is wrong with it? See one thing, in your current compiler,
the default argument is void, but
the same may not be true with other compilers or platforms. So you may end up
with portability issues! Keep in mind, A GOOD PROGRAMMER NEVER RELY ON
DEFAULTS. So please don’t rely on default things any more, clearly write that
your main function is not expecting any arguments by writing main(void).
You already have an idea that a
function can never work unless and until it is being called by any other
program component. This is true for main
also. The function main must be called by someone! Who is that? Who calls
main function? It is the operating system who calls main function. Now the
thing is, your operating system may decide to do any other job according to the
success or failure of running your main function. It may so happen that if your
program runs successfully, operating system will call a program P1, and if your
program does not work, your operating system will call another program P2. So
what if your operating system has no idea that your program did run actually or
not? There lies the importance of return type of main. Your main function
should return something to your operating system back to indicate whether it
has ran successfully or not. The rule is if operating system gets an integer 0
back, it takes as main has ran successfully and any non zero value indicates
the opposite. So your main program should return an integer. Hence you should
write int main(void). Just to add
with this, don’t forget to return 0 at
the end of your main program so that, if all of your codes run successfully,
your main function is capable of returning 0 to the operating system back,
indicating that it was a success.
In this point you may argue, that
your programs runs quite well even if you do not provide return type to main
and you just write main(void). How come
it is possible?
Once again, at your age, you are
writing just some of trivial academic codes. The situations here are not such
critical that your operating system may decide any other thing depending on the
success or failure of your main(). But,
in near future, you are to write such critical programs, so get prepared from
now.
Cool, so to conclude, int main(void) is perfect to write and I
encourage you to write like that along with a return 0 at the last line.
Now i am paying much more attention as it has come to "C".
ReplyDeleteThe first proper concept i have learnt was " why
int main (void)" ....
Sougata, many thanks for your comment
DeleteSir, will it be perfect to write "public static int main(String []args)" & then returning 0 at the last line in case of Java?
ReplyDelete