954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why NOT void main()

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?

iamboredguy
Newbie Poster
23 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

Operating systems such as UNIX require applications to return an integer value.
0 usually meaning success and non-zero meaning an error.

hivework
Newbie Poster
18 posts since Mar 2004
Reputation Points: 13
Solved Threads: 0
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

XianBin
Newbie Poster
24 posts since Aug 2004
Reputation Points: 15
Solved Threads: 0
 

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
#include


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*/

}

sureshsigera
Newbie Poster
4 posts since Sep 2004
Reputation Points: 10
Solved Threads: 1
 

People please! Is this a hosted implementation? Do you excect printf to do anything? Then void main is incorrect. Period.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Why is it necessary to have void main() in Java? Any takers?

madhav_2k
Newbie Poster
5 posts since Apr 2006
Reputation Points: 9
Solved Threads: 0
 

>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.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

void main()

{

unsigned int i=65537;

float j=2.25;

printf("%d",i*j);

getch();

}

sujitkumarsingh
Newbie Poster
1 post since Sep 2010
Reputation Points: 4
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You