code块的MinGW和%N转换字符 [英] Code blocks MinGW and the %n conversion character

查看:172
本文介绍了code块的MinGW和%N转换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个转换字符数有以下code到目前为止打印的字符数。

I am trying to use a conversion character to count the number of chars printed so far with the following code.

#include <stdio.h>
int main(void) {
    int n;
    printf("%s: %nFoo\n", "hello", &n);
    printf("%*sBar\n", n, "");
}

在正常工作,这code打印Hello美孚,然后在下一行酒吧是富相一致。

When working correctly this code prints Hello Foo, then on the next line Bar is aligned with Foo.

在与海湾合作委员会在我的Linux机器编译这工作得很好。然而,在code块使用我的Windows PC%N MinGW的不工作当编译,以及所有打印为您好:酒吧所有在一行上。为什么这么和我能解决这个问题?

This works fine when compiled with GCC on my linux box. However, when compiled using minGW in code blocks on my Windows PC %n does not work, and all that is printed is hello: Bar all on one line. Why so and can I fix this?

推荐答案

使用编译选项 -D__USE_MINGW_ANSI_STDIO

例如

>gcc prog.c -D__USE_MINGW_ANSI_STDIO

>clang prog.c -D__USE_MINGW_ANSI_STDIO


另一种解决方案是把这个命令的第一个导入语句上面,像这样(见下文)。这将确保ANSI I / O标准pferred过微软的$ P $。在这个环节这里更多信息。

#define __USE_MINGW_ANSI_STDIO 1
#include <stdio.h>

这篇关于code块的MinGW和%N转换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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