Solved

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

Posted on 2008-10-08
6
8,563 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
6 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
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
Comment Utility
_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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Too slow :) ... sorry for the repeated points
0
 
LVL 3

Expert Comment

by:wktang83
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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: …
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 use and create new code templates in NetBeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now