understanding main() and _tmain

T

tonyjeffs2

//int _tmain(int argc, _TCHAR* argv[]) // main for windows
int main(int argc, char *argv[]) // ... or main

{
cout <<argv[0]<<endl;
return 0;
}

//run at command line: test.exe cat dog

With main the output is "test.exe" which I'd expect,
but a similar program using _ tmain outputs a number which I think is
the address of the 't' in test.exe. What is the logic behind this -
what's happening?

Thanks

Tony
 
C

Cholo Lennon

_tmain is a macro that expand to main o wmain according to _UNICODE macro. In
VC2005, By default , a program is unicode enabled (UNICODE and _UNICODE defined)
so your _tmain macro expands to wmain (not main) and _TCHAR expands to wchar_t.
The problem is that you are using 'cout' (which is prepared to work with
single wide strings) with a double wide string.

// This should work
#include <iostream>
#include <tchar.h>

#ifdef _UNICODE
#define _tcout wcout
#else
#define _tcout cout
#endif

using namespace std;

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

Regards
 
T

tonyjeffs2

_tmain is a macro that expand to main o wmain according to _UNICODE macro. In
VC2005, By default , a program is unicode enabled (UNICODE and _UNICODE defined)
so your _tmain macro expands to wmain (not main) and _TCHAR expands to wchar_t.
The problem is that you are using 'cout' (which is prepared to work with
single wide strings) with a double wide string.
<....>

Thank you - I understand that!
Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top