int main(void) vs void mainMeaning of all these
18 Replies - 21007 Views - Last Post: 08 November 2009 - 02:07 AM
#1
int main(void) vs void main
Posted 06 November 2009 - 02:50 AM
int main (void)
void main (void)
int main()
void main()
I am only sure about the fourth one one, ie void main(). That it is not supposed to return any value and no need for return 0; in the end. If someone can please clear this would be very grateful. And also tell me in which of the above am I supposed to add return 0; at the end. Thank you.
Replies To: int main(void) vs void main
#2
Re: int main(void) vs void main
Posted 06 November 2009 - 03:02 AM
there is no difference between:
int main (void) and int main()
and there is no difference between:
void main (void) and void main ()
Quote
for int main() you should add return 0
This post has been edited by bita: 06 November 2009 - 03:03 AM
#3
Re: int main(void) vs void main
Posted 06 November 2009 - 03:21 AM
int main() {
//codes
return 0;
}
I believe that it's better to use "int" for main, because when you return a zero, the operating system knows that your program is terminated with success code ("0"). Hope I helped.
#4
Re: int main(void) vs void main
Posted 06 November 2009 - 06:46 AM
void main is not standard, and should be refused by any decent compiler as such.
Links for further reading:
http://www.parashift...e.html#faq-29.3
http://www.eskimo.co...ain.960823.html
http://users.aber.ac.../voidmain.shtml
http://www.safercode...-void-main.html
#5
Re: int main(void) vs void main
Posted 06 November 2009 - 07:19 AM
#6
Re: int main(void) vs void main
Posted 06 November 2009 - 07:42 AM
void main (void) -- non-standard entry point ignore return value AND command line arguments.
int main() -- short-hand for int main(void).
void main() -- short-hand for void main(void).
Technically in a modern OS like windows/linux/unix/BSD/mac os/DOS/OS2/Geos etc. programs accept arguments and return values. That means that the real entry point for most programs is:
int main(int argc, char *argv[])
Now because people are lazier than computers this often gets shortened to:
int main()
because we are not using the argc and argv arguments -- but it turns out that they are still there! The OS still passes the arguments, you have just chosen to ignore them.
for example in this thread the poster was trying to call int main(int) recursively but was confused at the results -- because the syntax int main(int a) picks up the first argument passed to main() which (as it turns out) is the number of arguments on the command line.
again with void main() -- just because you have chosen to ignore the return value, does not mean that the OS does -- your program still returns a value, you just don't have control over what value gets returned.
#7
Re: int main(void) vs void main
Posted 06 November 2009 - 07:48 AM
main()
{
// some code
}
the compiler is supposed to interpret it as an integer return type. However, the standard has been revised, and most modern compilers will force you to explicitly set main() to be an int return type. The purpose of the return type is to pass either the EXIT_SUCCESS value, 0, or EXIT_FAILURE value 1.
Many of the other main function headers, like void main(), are sloppy colloquialisms that have to be supported for code compatibility, but in terms of strict syntax adherence, are incorrect.
This post has been edited by noyesa: 06 November 2009 - 07:51 AM
#8
Re: int main(void) vs void main
Posted 06 November 2009 - 08:26 AM
khizaraq, on 6 Nov, 2009 - 01:50 AM, said:
int main (void)
void main (void)
int main()
void main()
I am only sure about the fourth one one, ie void main(). That it is not supposed to return any value and no need for return 0; in the end. If someone can please clear this would be very grateful. And also tell me in which of the above am I supposed to add return 0; at the end. Thank you.
Current best-practices says that all functions, including main, should have declarations for function parameters and return types. For example,
void main(void) {
//some code
}
would be used if main takes no parameters and returns no value. However, since your program may have a situation where it needs to communicate a program termination code, typically an error code, that code should be returned by main (in this case). So, the data type indicator in front of main should be the same data type as your return value. Again, this is typically an int. Your code might look like this:
int main(void) {
//some code
return(0)
}
as was pointed out by another poster.
I disagree that leaving the parameter list empty rather than use void is better. First it is not what is recommended by software design best practices. More specifically, the whole idea in writing code professionally is to create not only robust code, but code that is readable by those who will be maintaining the code. It was reported some time ago that over 60% of the cost of software is in its maintenance. Secondly, if your integrated development environment (IDE) is set to strict checking, you will probably get a warning message about the prototyping for main. I doubt that in the executable code there would be any difference between the two, but the prototyping and strict checking are there to help you from shooting yourself in the foot.
Remember, C is a context-sensitive language, which means that the programmer has to always be thinking from the perspective of the compiler as well as from the perspective of the program under design. This split focus can let bugs in. This suggests that to swing the majority of your focus to the program design, you should become familiar with and use the best practices for software engineering in general and coding best practices for your particular language. Sadly (or happily), more than one best practices exists for coding in C. Since IBM is noted for being very conservative, I suggest theirs, but used as a guide and not the law. You can google "c programming best practices" and choose among the many listed. Also, one of many excellent reference books for C is Deitel & Deitel's "C how to program" as they tend to show and use best practices, and another good reference book, especially for C++, is Andrei Alexandrescu's "Modern C++ Design: Generic Programming and Design Patterns Applied."
Good luck in your new career!
~jiangshi
#9
Re: int main(void) vs void main
Posted 06 November 2009 - 08:44 AM
#10
Re: int main(void) vs void main
Posted 06 November 2009 - 08:52 AM
#11
Re: int main(void) vs void main
Posted 06 November 2009 - 09:21 AM
khizaraq, on 6 Nov, 2009 - 07:44 AM, said:
I suggest using a return statement at the end of your code. Saying that it is incorrect not to do so may be overstating it a bit, but I think most best practice guides will state that it is required if you are going to follow best practices. Besides, it really does not make much sense to put an int in front of main and then not return some int value. This guide is really for all functions, not just main. It is my experience that when I have the highest level of type checking turned on in my IDE, I get a warning if I do not have a return statement for main, and I have declared main to have a return type of int. This is IDE dependent, so you may not get anything or you may get an error.
I believe that someone else stated here that using void in front of main is not a good practice, and I agree. Regardless of the level of program I am writing or the intended OS, I always use a return value and put the corresponding data type declaration in front of main.
int main(void){
printf("hello, world!\n");
return 0;
}
By doing so, it may also facilitate the OS's orderly termination of my program. Some OSs may be more sensitive to that than others.
~jiangshi
#12
Re: int main(void) vs void main
Posted 06 November 2009 - 01:41 PM
There is no best practices rule here. Whether you add in a return 0 or not at the end is completely up to you. The standard is pretty clear about this aspect of C++.
jiangshi said:
#13
Re: int main(void) vs void main
Posted 07 November 2009 - 05:23 AM
#14
Re: int main(void) vs void main
Posted 07 November 2009 - 05:39 AM
khizaraq, on 6 Nov, 2009 - 09:44 AM, said:
When you declare your function as type int, then it needs to return that value type. Otherwise, why bother declaring the function of a specific data type at all?
Ints return ints,
Chars return chars,
Voids return nothing.
Simple
#15
Re: int main(void) vs void main
Posted 07 November 2009 - 09:22 AM
A. Your too lazy to type it out.
B. Your on a deadline and don't have time to type it out.
C. Your writing obfuscated code and don't want someone analyzing your code to see a return statement.
D. Your a parapalegic typing out characters one letter at a time using a stick manipulated with your lips... and want to save the extra keystrokes.
E. typing out "return 0;" requires .2 seconds and you average 1500 programs a year and figure you can save yourself 5mins a year by not typing out "return 0;"
F. "return 0;" adds 11 characters (including eol) to each file and so by not adding it to your programms you are saving space on the hard-drive... leaving more room for porn.
Basically its pure lazyness to leave out the return statement. These days an IDE can put the whole shell of main() in for you with 2-key stroks. (for me I type "ma" then ctrl-shift-space and my editor inserts:
#include <iostream>
using namespace std;
int main() {
return 0;
}
this process takes less than a second. Editors like emacs/vim/VS/Dev-C++/Code::blocks/Eclipse/NetBeans etc. are all capable of HELPING you avoid as much typing as you would like. Heck I wrote a program in C# the other day and only actually typed a few lines -- code generation and point-and-click programming did most of it for me.
As far as I am concerned any programmer who can't add a "return 0;" to the end of main is too lazy to breath and should be put out of their misery.

New Topic/Question
Reply



MultiQuote






|