I am using Visual C++ 2010, and the code is:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[]) {
printf("step 0: %s\n", argv[0]);
int d;
scanf("%d",&d);
return 0;
}
When I choose _UNICODE, the output is “D”,
and when I choose _MBCS, it works well and the output is “D:\VCTest\c1006.exe”
Why is it wrong in _UNICODE?
How can I use the same code for both _UNICODE and _MBCS ?
When compiling a Unicode project,
_TCHARiswchar_t, notchar.The
%sformat specifier expects its corresponding argument to be achar const*; you are passing awchar_t*, thus the unexpected result.You can use
_tprintf, which will select the rightprintffunction (printforwprintf) based on whether the project is being built as ANSI or Unicode. If you want to useprintf, you can use the%lsformat specifier for wide strings, but then you have to use different format strings depending on whether the project is built as ANSI or Unicode.Alternatively, just use Unicode and don’t use
_TCHARand friends at all.