Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Difference between Void and Int Main

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shirotaka
Advanced Cheater
Reputation: 0

Joined: 18 Sep 2007
Posts: 87

PostPosted: Tue Apr 15, 2008 10:49 pm    Post subject: Difference between Void and Int Main Reply with quote

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
View user's profile Send private message
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Tue Apr 15, 2008 11:02 pm    Post subject: Re: Difference between Void and Int Main Reply with quote

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
View user's profile Send private message MSN Messenger
shirotaka
Advanced Cheater
Reputation: 0

Joined: 18 Sep 2007
Posts: 87

PostPosted: Tue Apr 15, 2008 11:53 pm    Post subject: Re: Difference between Void and Int Main Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Apr 16, 2008 2:28 am    Post subject: Reply with quote

Search. Result: eg. http://forum.cheatengine.org/viewtopic.php?t=220213
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Wed Apr 16, 2008 3:55 am    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Apr 16, 2008 4:16 am    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Apr 16, 2008 5:20 am    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Apr 16, 2008 8:57 am    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Apr 16, 2008 10:54 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Apr 19, 2008 12:02 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Sat Apr 19, 2008 3:05 am    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Apr 19, 2008 4:41 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites