且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Visual Studio 2013 中将 stdint 类型与 _tprintf 一起使用?

更新时间:2021-09-20 00:14:07

根据当前的 C 标准1,只有一个字符序列(读作:字符串)必须以 a 为前缀一个编码前缀,其余的都被视为具有相同的前缀,并连接成一个字符串.

According1 to the current C standard, only one of the character sequences (read as: a string) must be prefixed by a an encoding prefix, and the rest of them are treated to have the same prefix, and are concatenated into a single string.

编码前缀由_T宏决定.如果未定义 UNICODE,它将解析为空,否则它将在参数前面加上 L.

The encoding prefix is determined by the _T macro. It will resolve to nothing, if UNICODE is not defined, otherwise it will prepend L to the argument.

解决方案是在第一个字符串上使用 _T 宏,其余不使用宏,它们将使用相同的编码:

The solution would be to use the _T macro on the first string, no macro on the rest, and they will use the same encoding:

_tprintf(_T("The size of %s is %") PRIu32 "\n", fileName, fileSize);

但是您使用的 Visual Studio 版本不符合 C99,因此缺少此功能.这似乎已在 Visual Studio 2015 中修复.

But the Visual Studio version you're using isn't C99 compliant, so this feature is missing. It seems this was fixed in Visual Studio 2015.

在标准中的示例2中演示了相同的用法.

The same usage is demonstrated in the example2 in the standard.

1(引用自:ISO/IEC 9899:201x 6.4.5 字符串文字 5)
在翻译阶段 6 中,由任意序列指定的多字节字符序列相邻字符和相同前缀的字符串文字标记连接成一个单个多字节字符序列.如果任何令牌具有编码前缀,则生成的多字节字符序列被视为具有相同的前缀;否则,它被视为字符串文字.是否具有不同前缀的宽字符串文字令牌可以连接,如果是这样,结果多字节字符的处理序列是实现定义的.

1 (Quoted from: ISO/IEC 9899:201x 6.4.5 String literals 5)
In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens has an encoding prefix, the resulting multibyte character sequence is treated as having the same prefix; otherwise, it is treated as a character string literal. Whether differently-prefixed wide string literal tokens can be concatenated and, if so, the treatment of the resulting multibyte character sequence are implementation-defined.

2(引用自:ISO/IEC 9899:201x 7.8.1 格式说明符 7 的宏)
wprintf(L"最大整数值为%020" PRIxMAX "\n", i);

2 (Quoted from: ISO/IEC 9899:201x 7.8.1 Macros for format specifiers 7)
wprintf(L"The largest integer value is %020" PRIxMAX "\n", i);