Solved

Difference between "void main()" and "int _tmain(int argc, _TCHAR* argv[])"?

Posted on 2008-10-08
6
8,824 Views
Last Modified: 2013-12-14
Hello. I am learning C++ and I am following a series of educational videos + some books. All these
books declare the main function as

void main()

However, when I downloaded and installed VC++ Express 2008 the default main declaration is

int _tmain(int argc, _TCHAR* argv[])

The programs run fine as well, but I really don't understand what's the difference, and it really bugs me the fact that I am using something I don't really understand. Adding to the confusion there's the fact that the videos I am
using use Microsoft Visual C++ as well (although I can tell it is not the 2008 version, probably an older one.)

So, basically what I would really know is:

1. The difference between the two declarations
2. Which one of the two should I consider "standard"
3. If there's really no standard to speak of,  but it is something strictly linked to my actual development environment, which in this case is VC++ 2008 Express.

Thank you. :)

(By the way, just in case it wasn't already clear, I'm a beginner as a beginner could be.)
0
Comment
Question by:broocrypt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 22669762
void main()

is the default implementation for an ascii/ansi application, it could be also:

int main(int argc, char *argv[])

if you need to read the command-line arguments. The following implementation:

int _tmain(int argc, _TCHAR* argv[])

is the default implementation for Unicode application, but can be used for ascii/ansi too. If you won't use the command-line arguments, just ignore them.
0
 
LVL 35

Assisted Solution

by:torimar
torimar earned 100 total points
ID: 22669780
_TCHAR* argv[] is a pointer to the argument that the programme is started with, like:
"myexe.exe thisfile"  or "myexe.exe -a -b"

argc contains the number of arguments (argv) that the exe was started with.

'int' _tmain instead of 'void' will most likely refer to the fact that VC++ executables always return an exit code on ending in order to communicate to the OS whether they were correctly ended or aborted etc

As you have seen, your exercises work with this template just as well. It's just a more difficult or exact way of putting it.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 22669852
1. The difference between the two declarations
main () is the function from which execution of your code starts - the user code entry point
void main() means main function returns nothing - void
() after main indicate that the function has empty argument list - does not take any argument.

_tmain() is microsoft specific entry point for user code that resolves to main. This would not work in other programming environments.
http://msdn.microsoft.com/en-us/library/6wd819wh.aspx
It returns an int and takes 2 arguments - int argc the number of arguments, _TCHAR * argv[] an array of strings -
he actual command line arguments

2. Which one of the two should I consider "standard"
Neither ... standard says
int main (int argc, char * argv[])

3. If there's really no standard to speak of,  but it is something strictly linked to my actual development environment, which in this case is VC++ 2008 Express.
I guess I answered that above
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22669929
Too slow :) ... sorry for the repeated points
0
 
LVL 3

Expert Comment

by:wktang83
ID: 22669936
Yes I agree with sunnycoder.

The only standard in C++ is
int main()
or
int main(int argc, char* argv[]) (if you want to pass in parameters through command line)

This (int _tmain(int argc, _TCHAR* argv[])) is Windows specific, and is specific to the Visual Studio environment only.
0
 

Author Closing Comment

by:broocrypt
ID: 31504255
Thank you everyone. Super clear now. It seemed unfair to give all the points to just one expert, since almost everyone added something to others' answers and viceversa. I divided them based on usefulness and effort. Hope you guys don't mind. Don't worry - as a beginner I will post a LOT more questions to gain points with. :))
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question