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

Posted on 2008-10-08
Medium Priority
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.)
Question by:broocrypt
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 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.
LVL 35

Assisted Solution

torimar earned 400 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.
LVL 45

Accepted Solution

sunnycoder earned 1200 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.
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

LVL 45

Expert Comment

ID: 22669929
Too slow :) ... sorry for the repeated points

Expert Comment

ID: 22669936
Yes I agree with sunnycoder.

The only standard in C++ is
int main()
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.

Author Closing Comment

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. :))

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

607 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