-
September 17th, 2011, 08:17 AM
#1
int main() / void main()
Sorry guys,
i know there are some threads around on the internet sort of explaining why we can not use void main()
i sort of understand it, but somehow the explanations i found on the internet do not make it crystal clear to me....
Can someone please tell me in a noob way what the big difference is, both will work, but i read every where the void main() is a absolute NO...
-
September 17th, 2011, 08:24 AM
#2
Re: int main() / void main()
The explanation is quite simple. int main is the only correct way to declare main because that's what the language standard says.
-
September 17th, 2011, 08:26 AM
#3
Re: int main() / void main()
-
September 17th, 2011, 08:36 AM
#4
Re: int main() / void main()
Originally Posted by Codeplug
thanks, i copied some lines:
Code:
Actually, main can be declared in any of the four following ways:
int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char** argv)
Unless a program uses command-line arguments, one of the first two would suffice.
i've created a program with many functions with: void function(){} The above lines say unless a program uses command-line argumentes int main and int main(void) will be enough.
i use some user_input in my functions and all my functions are like: void function()
so what about:
int main(int argc, char *argv[])
int main(int argc, char** argv)
-
September 17th, 2011, 10:45 AM
#5
Re: int main() / void main()
??
int main(int argc, char *argv[])
int main(int argc, char** argv)
are for handling command line arguments. You know there's a difference between command line args and user input?
-
September 17th, 2011, 01:59 PM
#6
Re: int main() / void main()
Originally Posted by Amleto
??
int main(int argc, char *argv[])
int main(int argc, char** argv)
are for handling command line arguments. You know there's a difference between command line args and user input?
well never used it that way (yet)
-
September 17th, 2011, 02:32 PM
#7
Re: int main() / void main()
Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.
And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too.
A bit of searching found that.
in simple terms it says main()has to return 0 or EXIT_SUCCESS to show the program has run successfully and void will not let the return of a proper value that is needed
Last edited by medicalsalt; September 17th, 2011 at 02:36 PM.
-
September 18th, 2011, 04:46 AM
#8
Re: int main() / void main()
Originally Posted by medicalsalt
Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.
And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too.
A bit of searching found that.
in simple terms it says main()has to return 0 or EXIT_SUCCESS to show the program has run successfully and void will not let the return of a proper value that is needed
This was very helpfull, thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|