Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
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
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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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. …
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…
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

609 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