How should I define main() function - with return type void or int?
Actually my teacher says that it's better to use void type '.' main function is called implicitly and we don't know expected return type from caller.
But I have seen a whole lot of programs use type int.
What should I use and why?
Thanks. Now I do know that return type must be int.
BUT besides the fact that it's standard is there any more logical reason for this ?
@firedraco
The site says that the declaration : int main(int argc, char** argv) is a good practice .
Is this a valid declaration even ?
If yes then please explain how are we allowed to define parameters for an implicitly called function ?
argc argv are used to manage the argument passed to your program: argc is the number of argument passed, argv is an array of C strings containing the values
eg:
From command line:
myprogram.exe arg1 arg2
values:
argc = 3
argv[0] = "path/myprogram.exe" (argv[0] is always the name of the executable file)
argv[1] = "arg1"
argv[2] = "arg2"
Yes there is logical reason. Main must have a return of some kind so it can communicate to the os how things went in the execution. Void is not only bad style but it risks depriving the OS of that data. refer to this article, which was shown to me in Duoas's issues in console programming: http://www.gidnetwork.com/b-66.html
Many MS windows based programs use void, or even no return type. simply main(), but the problem with this is portability.
Many may argue that writing for linux/solaris/bsd is obsolete anyway, but consider this. Turn on your tv you can't go 10minutes without a Mac add popping up. And being that mac is based on linux, porting to mac is just as easy as porting to linux. and due to the continued and increasing mac adds and mac users you would be doing yourself a great injustice, to base your work only on windows platform.
How hard is it to make main() return an int? What reason is there to not do it every time? Why intentionally break the standard? What is there to gain?
Well Thanks everyone. I am glad that I now have my concepts clear.
@Disch
I don't intend to break the standard, it's just that I don't like to do anything blindly just because it's a standard or followed by everyone.
I don't intend to break the standard, it's just that I don't like to do anything blindly just because it's a standard or followed by everyone.
When we say "standard" we mean that it must be that or what are you typing is not C++ but some other language.
Said that, is good to try to understand why the standard says so