Here’s my code:
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 1;i<argc;i++) printf("Argument %d:%s\n",i,argv[i]);
// output = Argument 1:param
for (int k = 1; k < argc; k++) cout << "Argument " << k << ": " << argv[k];
// output = Argument 1: 00BF5878
return(0);
}
My question is: why do I see different outputs on cout and printf?
You see an address in the output from
coutbecause you have built the program withUNICODEor_UNICODEdefined. Probably by way of a setting in a Visual Studio project. Then_tmainexpands to MIcrosoft’s non-standardwmain, and_TCHARexpands towchar_t.And
coutdoesn’t know how that a pointer towchar_tis supposed to point to a null-terminated string of wide characters.I am not sure why that doesn’t happen with
printf. Lemme check.OK I have checked and your
printfis not printing “param” as you indicate it does.Here’s the corrected code I checked with:
And here’s the result:
In other words, the apparent conundrum is entirely caused by your inaccurate reporting of results.
Solution: instead of using Microsoft’s non-standard
wmain, and in particular instead of using the now totally meaningless Windows 9x support macros!, use a standard C++main.