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?

Dani AI

Generated

Short answer: standard C++ expects main to return an int and the returned value is the program's exit status that the operating system (or invoking environment) receives. void main() is non-conforming in C++ and therefore non-portable; some compilers accept it as an extension, but that is not the language rule and can hide bugs. See the C++ rules on the main function. (en.cppreference.com)

Clarification and historical notes: C++ only permits int-returning forms of main (with or without argc/argv); if control reaches the end of a well-formed int main, it is treated as return 0;. C's rules evolved: older C variants left this ambiguous, but since C99 the behavior of falling off main was specified and is equivalent to returning zero; non-int main signatures lead to implementation-defined or unspecified results. See the C and C++ language references for details. (en.cppreference.com)

Why people react strongly: using void main() hides portability problems and can interact badly with platform conventions (exit codes, shells, parent processes). Historically some compilers (Turbo C, early vendors) accepted void main() and teaching examples propagated that habit — but that is legacy, not a standard guarantee. For hosted (OS-based) programs prefer the standard forms; freestanding/embedded runtimes are a special case and may define different entry points. (stackoverflow.com)

Practical advice (apply immediately): prefer a standard signature and macros for clarity:

#include <cstdlib>

int main(int argc, char *argv[])
{
// program logic
return EXIT_SUCCESS;
}

Use EXIT_SUCCESS/EXIT_FAILURE or 0/nonzero to communicate success/failure to callers. If a compiler still warns, enable strict standard conformance (-std=... / pedantic) and fix the signature rather than silencing the warning. For portable, maintainable code avoid void main(), nonstandard headers like conio.h, and mismatched printf formats — those are separate sources of UB that will confuse debugging. (gnu.org)

Relevant posts on this thread (ties): and are on the right track about return values and OS conventions; and show why examples can be misleading — the presence of void main() in old tutorials does not make it correct. (en.cppreference.com)

Recommended Answers

All 9 Replies

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

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

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.

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.

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

}

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

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

commented: Yet another off-topic bump of a 2+ year old thread - because java has fuck-all to do with C or C++ -1

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

commented: I know this is a late REPlie, but your post is definitely worth it :P +23

void main()

{

unsigned int i=65537;

float j=2.25;

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

getch();

}

commented: what is that supposed to be??? -6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.