What is the difference between _tmain() and main() in C++?
2012-07-10 19:04
615 查看
_tmaindoes not exist in C++.
maindoes.
_tmainis a Microsoft extension.
mainis, 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[]);
Microsoft has added a wmain which replaces the second signature with this:
int wmain(int argc,wchar_t* argv[]);
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.
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_targument, 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.
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
\0followed by the ASCII value.
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.
In general, you have three options when doing Windows programming:
Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the
-Wversion of the function. Instead of CreateWindow, call CreateWindowW). And instead of using
charuse
wchar_t, and so on
Explicitly disable Unicode. Call main, and CreateWindowA, and use
charfor strings.
Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_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.
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.
相关文章推荐
- What exactly is the difference between “pass by reference” in C and in C++?
- what is the difference between static and normal variables in c++
- What is the difference between provider network and self-service network in OpenStack?
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- what is the difference between _source and _all in Elasticsearch
- What are the differences between struct and class in C++?
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- What is the difference between @staticmethod and @classmethod in Python?
- Whats is the difference between train, validation and test set, in neural networks?
- What is the difference between the ways to implement inheritance in javascript.
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- What are the differences between a pointer variable and a reference variable in C++?
- what is the difference between definition and declaration in c
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?
- OpenStack: What is the difference between Block storage and Object storage?Also, In what way they ar
- In PHP5, what is the difference between using self and $this? When is each appropriate?
- macro与inline的区别 What is the difference between macro and inline?
- What is the difference between a soft reference and a weak reference in Java?
- What are the differences between a pointer variable and a reference variable in C++?
- C++ Memory Management : What is the difference between malloc/free and new/delete?