User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,237 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,204 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2737 | Replies: 8
Reply
Join Date: Aug 2004
Location: Dubai
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Why NOT void main()

  #1  
Sep 2nd, 2004
I don't get why C++ pros see red when they see void main(). Can someone explain why intmain() is good and void main() bad?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Why NOT void main()

  #2  
Sep 2nd, 2004
Not declaring a function's return value is an error in C++, whether the function is main or not. In C99, the October 1999 revision to Standard C, not declaring a function's return value is also an error (in the previous version of Standard C an implicit int was assumed as the return value if a function was declared/defined without a return value). But the usual requirement of both Standard C++ and Standard C is that main should be declared to return an int. In other words, this is an acceptable form of main:
int main(void) { /* ... */ }

The problem is that this code declares main to return a void and that's just no good for a strictly conforming program. Neither is this:

implicit int not allowed in C++ or C99
main() { /* ...Whatever... */ }


Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Mar 2004
Location: California
Posts: 17
Reputation: hivework is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Why NOT void main()

  #3  
Sep 2nd, 2004
Operating systems such as UNIX require applications to return an integer value.
0 usually meaning success and non-zero meaning an error.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,645
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 143
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why NOT void main()

  #4  
Sep 2nd, 2004
Originally Posted by iamboredguy
I don't get why C++ pros see red when they see void main(). Can someone explain why intmain() is good and void main() bad?
In this reply, I posted 6 links to explanations of why it is considered wrong.
Reply With Quote  
Join Date: Aug 2004
Posts: 23
Reputation: XianBin is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

Re: Why NOT void main()

  #5  
Sep 4th, 2004
the first, you mustn't have any return statement in your main() function as if you use void main() declare. If you use int main() function, you must return a int value, otherwise your complier will prompt a warning.
Reply With Quote  
Join Date: Sep 2004
Location: Sagianw MI
Posts: 4
Reputation: sureshsigera is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
sureshsigera's Avatar
sureshsigera sureshsigera is offline Offline
Newbie Poster

Re: Why NOT void main()

  #6  
Sep 6th, 2004
In You Program You can Have Program like this

pro 1
void main()
{
/* */

}

pro 2

int main()
{

/* */

return 0;
}

in pro1 its showing you void main it's mean's RETURN NOTHING just a main Function with your code!! but in pro2 its haveing int main() function it's mean's that function return a value!! so if you put in void main function to return a value u will get an Error so best way is you have to use void main function in your program!!

example

#include <stdio.h>
#include <conio.h>


int call(int x,int y);

void main()
{
int num1,num2,ans;

clrscr();
printf("Enter the First number : ");
scanf("%d",&num1);
printf("\nEnter the Secound Number : ");
scanf("%d",&num2);

/*call the cal function using ans */

ans=call(num1,num2);

printf("The Add of num1 and num2 is %d",ans);

getch();
}

int call(int x,int y)
{
int z;

z=(x+y);

return (z); /* cal function go back to main function*/

}
Reply With Quote  
Join Date: Apr 2004
Posts: 3,645
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 143
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why NOT void main()

  #7  
Sep 6th, 2004
People please! Is this a hosted implementation? Do you excect printf to do anything? Then void main is incorrect. Period.
Reply With Quote  
Join Date: Apr 2006
Posts: 5
Reputation: madhav_2k is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
madhav_2k madhav_2k is offline Offline
Newbie Poster

Re: Why NOT void main()

  #8  
Mar 29th, 2007
Why is it necessary to have void main() in Java? Any takers?
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,505
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is online now Online
Vampirical Moderator

Re: Why NOT void main()

  #9  
Mar 30th, 2007
>Why is it necessary to have void main() in Java?
I'd say it's because Java applications run in a virtual environment, curtosey of the Java VM. So when a program finishes, it doesn't return to the operating system, it in fact returns to the VM. Therefore rules such as int main do not apply.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC