int main(void) vs void main

Meaning of all these

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 21007 Views - Last Post: 08 November 2009 - 02:07 AM Rate Topic: -----

#1 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

int main(void) vs void main

Posted 06 November 2009 - 02:50 AM

Hey. I am new to programming. We have just started in our first year of Electrical Engineering baisc programming in C++. I have come across multiple conventions regarding the main function prototype. I wish to know what is the best way to write and what the following mean.
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.

Is This A Good Question/Topic? 0
  • +

Replies To: int main(void) vs void main

#2 bita   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 21-April 09

Re: int main(void) vs void main

Posted 06 November 2009 - 03:02 AM

void type never returns anything but about int it always return a value.

there is no difference between:

int main (void) and int main()

and there is no difference between:

void main (void) and void main ()

Quote

And also tell me in which of the above am I supposed to add return 0; at the end. Thank you.


for int main() you should add return 0

This post has been edited by bita: 06 November 2009 - 03:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: int main(void) vs void main

Posted 06 November 2009 - 03:21 AM

Exactly as bita explained. In C++, it is better if you don't use "void" as the argument, just let it be empty. like this:
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.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: int main(void) vs void main

Posted 06 November 2009 - 06:46 AM

The standards (C++, C99) state the entry point to a standard C and C++ program is either int main() -- when command line arguments are not required/used -- or int main(int argc, char *argv[]) when command line arguments are used (athough the latter may be supplemented with further arguments). The main function returns to the caller the status of the program on exit.

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
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: int main(void) vs void main

Posted 06 November 2009 - 07:19 AM

Here's some more information on the topic.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: int main(void) vs void main

Posted 06 November 2009 - 07:42 AM

int main (void) - standard entry point ignore command line arguments.
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.
Was This Post Helpful? 0
  • +
  • -

#7 noyesa   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 27
  • Joined: 01-April 09

Re: int main(void) vs void main

Posted 06 November 2009 - 07:48 AM

The C++ standard prescribes that main return an integer. The main function is called by the operating system, which expects a response when the application terminates. You can indicate the condition of your program when it terminates using the return code. main(), according to the original standard, is impilcitly an int, so if you do something like:
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

Was This Post Helpful? 0
  • +
  • -

#8 ~jiangshi   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 06-November 09

Re: int main(void) vs void main

Posted 06 November 2009 - 08:26 AM

View Postkhizaraq, on 6 Nov, 2009 - 01:50 AM, said:

Hey. I am new to programming. We have just started in our first year of Electrical Engineering baisc programming in C++. I have come across multiple conventions regarding the main function prototype. I wish to know what is the best way to write and what the following mean.
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
Was This Post Helpful? 1
  • +
  • -

#9 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: int main(void) vs void main

Posted 06 November 2009 - 08:44 AM

Wow nice awesome replies. But I still have to ask one thing. If I use int main(void) do I have to add return 0 at the end or not? And is it INCORRECT not to? I mean I get it, the program works even without. But as stated above "The Best Practice". I am new to C++, and well want to get the knowledge of my basic straight. So question is if I use int main(void) then am I supposed to enter return 0 at the end? When I use it in a code, the code works just fine even if I do not enter return 0 at the end. Thanks a lot guys, please let me know this too.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: int main(void) vs void main

Posted 06 November 2009 - 08:52 AM

http://www.dreaminco...wtopic57495.htm
http://stackoverflow...not-exitsuccess
Was This Post Helpful? 0
  • +
  • -

#11 ~jiangshi   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 06-November 09

Re: int main(void) vs void main

Posted 06 November 2009 - 09:21 AM

View Postkhizaraq, on 6 Nov, 2009 - 07:44 AM, said:

Wow nice awesome replies. But I still have to ask one thing. If I use int main(void) do I have to add return 0 at the end or not? And is it INCORRECT not to? I mean I get it, the program works even without. But as stated above "The Best Practice". I am new to C++, and well want to get the knowledge of my basic straight. So question is if I use int main(void) then am I supposed to enter return 0 at the end? When I use it in a code, the code works just fine even if I do not enter return 0 at the end. Thanks a lot guys, please let me know this too.


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
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: int main(void) vs void main

Posted 06 November 2009 - 01:41 PM

Actually, the C++ standard explicitly states that there is an implicit return 0; at the end of main. main is a special function in C++ (it has two possible function signatures, and it has this exception about returning). And only in C++ by the way, not in C.

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:

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,
You are compiling C code. Wrong language.
Was This Post Helpful? 0
  • +
  • -

#13 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: int main(void) vs void main

Posted 07 November 2009 - 05:23 AM

Thank you so much for the help I think I really get it. At least reading all of this I am pretty sure about what int main and all is :D. Anyway I think considering that it doesn't make much of a difference but there is potential chance or errors when using void main (void) or void main() it is best to use int main(void) and add a return 0; at the end. At least if nothing I am telling my processor that OK dude the execution was successful you can chill, not that you care. lol. Thanks a lot guys.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: int main(void) vs void main

Posted 07 November 2009 - 05:39 AM

View Postkhizaraq, on 6 Nov, 2009 - 09:44 AM, said:

If I use int main(void) do I have to add return 0 at the end or not?

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 :)
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: int main(void) vs void main

Posted 07 November 2009 - 09:22 AM

First of all why would you want to leave off the return statement in main?

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2