是什么在C ++中_tmain()和main()的区别? [英] What is the difference between _tmain() and main() in C++?
问题描述
如果我跑我的C ++应用程序下面的main()方法万事OK:
If I run my C++ application with the following main() method everything is OK:
int main(int argc, char *argv[])
{
cout << "There are " << argc << " arguments:" << endl;
// Loop through each argument and print its number and value
for (int i=0; i<argc; i++)
cout << i << " " << argv[i] << endl;
return 0;
}
我得到了我希望和我的参数打印出来。
I get what I expect and my arguments are printed out.
不过,如果我使用_tmain:
However, if I use _tmain:
int _tmain(int argc, char *argv[])
{
cout << "There are " << argc << " arguments:" << endl;
// Loop through each argument and print its number and value
for (int i=0; i<argc; i++)
cout << i << " " << argv[i] << endl;
return 0;
}
它只是显示每个参数的第一个字符。
It just displays the first character of each argument.
是什么造成这种区别?
推荐答案
_tmain
中不存在C ++。 主
一样。
_tmain
does not exist in C++. main
does.
_tmain
是一个Microsoft扩展。
_tmain
is a Microsoft extension.
主
,根据C ++标准,程序的入口点。
它有两个特征之一:
main
is, according to the C++ standard, the program's entry point.
It has one of these two signatures:
int main();
int main(int argc, char* argv[]);
微软增加了一个wmain,与此代替第二签名:
Microsoft has added a wmain which replaces the second signature with this:
int wmain(int argc, wchar_t* argv[]);
然后,以使其更容易统一code(UTF-16)和多字节字符集之间切换,他们已经定义的 _tmain
,如果UNI code被启用,被编译为 wmain
,否则为主
。
And then, to make it easier to switch between Unicode (UTF-16) and their multibyte character set, they've defined _tmain
which, if Unicode is enabled, is compiled as wmain
, and otherwise as main
.
至于你的问题的第二部分,拼图的第一部分是你的主要功能是错误的。 wmain
应采取 wchar_t的
参数,而不是字符
。由于编译器不强制这个对主
的功能,你就会得到一个程序,其中 wchar_t的
字符串数组是传递到主
功能,其中除$ p $其中pts他们为字符
字符串。
As for the second part of your question, the first part of the puzzle is that your main function is wrong. wmain
should take a wchar_t
argument, not char
. Since the compiler doesn't enforce this for the main
function, you get a program where an array of wchar_t
strings are passed to the main
function, which interprets them as char
strings.
现在,在UTF-16,使用的字符集由Windows当启用统一code,所有的ASCII字符重新psented为字节对$ P $ \\ 0
后跟的ASCII值。
Now, in UTF-16, the character set used by Windows when Unicode is enabled, all the ASCII characters are represented as the pair of bytes \0
followed by the ASCII value.
此外,由于x86处理器是小尾数,这些字节的顺序进行交换,从而使ASCII值是第一位的,然后由一个空字节。
And since the x86 CPU is little-endian, the order of these bytes are swapped, so that the ASCII value comes first, then followed by a null byte.
和在字符的字符串,是如何串通常终止?是的,一个空字节。所以,你的程序发现一串字符串,每一个字节长。
And in a char string, how is the string usually terminated? Yep, by a null byte. So your program sees a bunch of strings, each one byte long.
在一般情况下,你有三个选择做Windows程序设计时:
In general, you have three options when doing Windows programming:
- 明确使用统一code(调用wmain,并为这需要焦炭相关的参数每个Windows API函数,调用该函数的
-W
版本。相反, CreateWindow的的,叫CreateWindowW)。而不是使用字符
使用wchar_t的
等 - 明确禁用的Uni code。通话为主,并CreateWindowA,并使用
字符
的字符串。 - 允许两者。 (呼叫_tmain和CreateWindow的,它决心主要/ _tmain和CreateWindowA / CreateWindowW),并使用替代字符/ wchar_t的TCHAR。
- Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the
-W
version of the function. Instead of CreateWindow, call CreateWindowW). And instead of usingchar
usewchar_t
, and so on - Explicitly disable Unicode. Call main, and CreateWindowA, and use
char
for strings. - Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_t.
这同样适用于由WINDOWS.H定义的字符串类型:
LPCTSTR解析为LPCSTR或LPCWSTR,并用于包括char或wchar_t的每一个其他类型,-T-版本始终存在可以替代使用。
The same applies to the string types defined by windows.h: LPCTSTR resolves to either LPCSTR or LPCWSTR, and for every other type that includes char or wchar_t, a -T- version always exists which can be used instead.
请注意,所有的这是微软具体。 TCHAR不是一个标准的C ++类,它是在WINDOWS.H中定义的宏。 wmain和_tmain也被微软定义只。
Note that all of this is Microsoft specific. TCHAR is not a standard C++ type, it is a macro defined in windows.h. wmain and _tmain are also defined by Microsoft only.
这篇关于是什么在C ++中_tmain()和main()的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!