| View previous topic :: View next topic |
| Author |
Message |
shirotaka Advanced Cheater
Reputation: 0
Joined: 18 Sep 2007 Posts: 87
|
Posted: Tue Apr 15, 2008 10:49 pm Post subject: Difference between Void and Int Main |
|
|
Well I'm using a couple of video training modules and all of a sudden they switched from INT main to VOID main. I'm wondering what the difference is except for the fact that they're using strings and pointers. Anyone care to explain?
EDIT: Forgot to add this is C++ |
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Tue Apr 15, 2008 11:02 pm Post subject: Re: Difference between Void and Int Main |
|
|
| shirotaka wrote: | Well I'm using a couple of video training modules and all of a sudden they switched from INT main to VOID main. I'm wondering what the difference is except for the fact that they're using strings and pointers. Anyone care to explain?
EDIT: Forgot to add this is C++ |
I'm pretty sure that with void you don't need to return a value.. _________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
shirotaka Advanced Cheater
Reputation: 0
Joined: 18 Sep 2007 Posts: 87
|
Posted: Tue Apr 15, 2008 11:53 pm Post subject: Re: Difference between Void and Int Main |
|
|
| Overload wrote: | | shirotaka wrote: | Well I'm using a couple of video training modules and all of a sudden they switched from INT main to VOID main. I'm wondering what the difference is except for the fact that they're using strings and pointers. Anyone care to explain?
EDIT: Forgot to add this is C++ |
I'm pretty sure that with void you don't need to return a value.. |
Oh thanks for the answer. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Wed Apr 16, 2008 3:55 am Post subject: |
|
|
Main(){} function doesn't have to be int unless you wanna return an integer value, if you wanna do a simple task, make it void.
some people use int main() as a defualt return value of 0. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Apr 16, 2008 4:16 am Post subject: |
|
|
| Rot1 wrote: | | Main(){} function doesn't have to be int unless you wanna return an integer value, if you wanna do a simple task, make it void. | It does. I suggest you to get familiar with the standards (C99 and we're talking about C++). |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Apr 16, 2008 5:20 am Post subject: |
|
|
| x0r wrote: | | Jani wrote: | | It does. I suggest you to get familiar with the standards (C99 and we're talking about C++). |
No it doesn't. I suggest you get familiar with the standards (ISO compliance has no direct implication for how main should be handled in regards to declaration/returning). | Well.. int main() and int main( int argc, char *argv[] ) are acceptable. W/e :) |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Apr 16, 2008 8:57 am Post subject: |
|
|
| Rot1 wrote: | Main(){} function doesn't have to be int unless you wanna return an integer value, if you wanna do a simple task, make it void.
some people use int main() as a defualt return value of 0. |
ERROR_SUCCESS = 0 right now. You're telling the shell that everything went cool. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Apr 16, 2008 10:54 am Post subject: |
|
|
Beings that everyone keeps talking about standards and never giving info based on the actual documents, here ya go:
http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
This is the most current C99 PDF. Section 5, sub-section 1.2.2.1 covers the program start definition. And guess what? 'int main()'
| Quote: | 5.1.2.2.1 Program startup
1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner. |
While 'void main()' may work and compile fine, it does not follow standards. _________________
- Retired. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Apr 19, 2008 12:02 am Post subject: |
|
|
Every other version prior to that document is the same, worded a bit different, but all boil down to 'int main()' being correct while 'void main()' being incorrect as it doesn't follow the standard.
Taken from the 1999 version since you seem to think that it's the only one allowed to be referenced to:
Section: 5.1.2.2.1
| Quote: | The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner. |
Either way, all documents from that point on all state the same thing, int main() being the correct standard. _________________
- Retired. |
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Sat Apr 19, 2008 3:05 am Post subject: |
|
|
Procedural programming(very briefly): every procedure returns a value, if the programmer wishes to ignore it, he\she declares it as void.
Treating main's return value as void, is wrong in it's concept - The operating system does wait for a return value from a running application\thread, and void forces it to be ERROR_SUCCESS. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Apr 19, 2008 4:41 am Post subject: |
|
|
Yeah, functions defined as void don't clear out registers unless you tell them to. This can cause issues though if the system is waiting for a certain value and is given another that is meant to do something else.
Easy way to test that:
| Code: | #include <windows.h>
#pragma comment( linker, "/ENTRY:main" )
void __declspec(naked) TestFunc()
{
//
// Ret required since the naked declare
// strips the function completely from
// all code.
//
__asm
{
mov eax, 0xFF
ret
}
}
int main()
{
__asm
{
xor eax, eax // Cleanup Eax Before We Start
call dword ptr[ TestFunc ] // Call Test function
xor eax, eax // Break here for testing :o
}
return 0;
} |
EAX carries over from the function since it is not told to clear up before the function returns. (Even with though __declspec(naked).) _________________
- Retired. |
|
| Back to top |
|
 |
|