user2023328
user2023328

Reputation: 125

Why do console C++ programs have a different starting code in Visual Studio?

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

I'm still a beginner in C++, but I was curious why do you have to include the stdafx.h library and why is the main function different from other IDEs?

Upvotes: 0

Views: 127

Answers (2)

user743382
user743382

Reputation:

The "stdafx.h" file is a file that's used for precompiled headers. If you want, you can remove it, include all headers directly, and turn off precompiled headers in your project options, and it will still work. It will just be slower.

_tmain and _TCHAR are macros that expand to either main and char, or to wmain and wchar_t, depending on whether Unicode is enabled in your project options. Standard C++ only has a int main(int argc, char *argv[]) declaration, and doesn't support any form that takes wchar_t, so you need implementation-specific extensions to get that to work. If you want, you can write int main(int argc, char *argv[]) if you don't want or don't need Unicode in your command-line arguments. And even with that prototype or with int main(void), you can use GetCommandLineW to get the command-line arguments in wide character form.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409346

It's very Windows specific and is because the Windows graphical programs have other arguments passed to the main function.

The reason for _tmain instead of the standard main is because of Unicode settings when building a Windows-specific console program. If you create a non Windows specific console program without Unicode support you should get the standard main function.

The "stdafx.h" is to handle precomiled headers. Some of the Windows system header files are very large, and parsing a header file for each source file (or rather translation unit) in the project can add a lot of time when building. By "precompiling" the headers, they are in a partially parsed state that is quicker to read and handle for the compiler.

Upvotes: 0

Related Questions