| | |
Why NOT void main()
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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
Hope this helps,
- Stack Overflow
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
•
•
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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*/
}
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>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
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. "Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ File Generator
- Next Thread: csv problem
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






