mingw printf尺寸说明字符h [英] mingw printf size specification character h

查看:140
本文介绍了mingw printf尺寸说明字符h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio具有大小和距离规范图表说我可以做这样的事情,使用h前缀指定单字节字符串,而不管printf还是wprintf:

Visual Studio has a size and distance specification chart that says I can do something like this, using the h prefix to specify single byte character string regardless of printf or wprintf:

int main()
{
char test[]= "abc";
_tprintf(_T("%hs\n"),test);
}


但是,如果我使用mingw gcc和-Wall编译相同的内容,则会收到以下警告:
warning: format '%hs' expects type 'short int *', but argument 2 has type 'char *'

But if I compile the same thing with mingw gcc and -Wall I get the following warning:
warning: format '%hs' expects type 'short int *', but argument 2 has type 'char *'

我在做一种可接受的方式来在mingw中指定一个单字节字符串吗?

Is what I'm doing an acceptable way to specify a single byte character string in mingw?

谢谢

编辑-下面已相当详细地回答了这个问题.简短的答案是肯定的,这是在mingw中指定单字节字符串的可接受方法,并且警告可以忽略.

推荐答案

您还将注意到Visual Studio文档说:

You'll also note that the Visual Studio docs say:

注意:h和l前缀是与char类型的数据一起使用时的Microsoft扩展名.

Note: The h and l prefixes are Microsoft extensions when used with data of type char.

我认为这是更准确的说法,当与'c''s'格式说明符(大写或小写)一起使用时."

I think that this would be more accurate to say, "when used with the 'c' or 's' format specifiers (upper or lower case)".

意识到GCC编译器确实不知道Microsoft的运行时如何处理printf()格式字符串,并且GCC给出的警告是针对它更通常使用的运行时量身定制的.使用MinGW进行构建时,将使用Windows随附的Microsoft C运行时msvcrt.dll(尽管MinGW确实为某些库函数提供了替代品或包装器).因此,有时对于printf()格式字符串的编译器认为可以的事情与提供给printf()的参数之间的混淆,以及运行时实际执行的操作之间会造成混淆.

Realize that the GCC compiler really doesn't know about how Microsoft's runtime deals with printf() format strings and the warning GCC is giving is tailored to the runtimes that it more normally uses. When building with MinGW, a Microsoft C runtime that's provided with Windows, msvcrt.dll, is used (though MinGW does provide replacements or wrappers for some library functions). So there is sometimes confusing between what the compiler thinks is OK for a printf() format string and the arguments provided to printf() and what the runtime will actually do.

例如,直到最近,在整数转换中使用ll格式化64位int类型在MinGW中无法正常工作.我不确定是否在MinGW支持功能中发生了此问题的修复",或者是否已更新msvcrt.dll以支持ll修饰符(我怀疑msvcrt.dll已更新-我必须检查.. ).

For example, until recently, using ll on an integer conversion to format a 64-bit int type wouldn't work correctly in MinGW. I'm not sure if the 'fix' for that occurred in the MinGW support functions or if msvcrt.dll was updated to support the ll modifier (I suspect that msvcrt.dll was updated - I'll have to check...).

无论如何,这归结为,无论GCC发出有关printf()格式字符串的警告,您极有可能需要使用MSVC文档作为格式字符串,因为它是MSGW运行时,MinGW将使用.如果您不喜欢生成warngin,则可能需要考虑在MinGW构建脚本中添加-Wno-format选项.

Anyway, what this boils down to is that regardless of the warnings that GCC gives about the printf() format string, in all likelihood you'll need to use the MSVC docs for format strings, since its an MS runtime that MinGW will be using. If you don't like the warngin being generated you might want to consider adding the -Wno-format option to your MinGW build script.

这篇关于mingw printf尺寸说明字符h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆