incre在c语言,Cython print()在C printf()之前输出,即使是在C printf()之后

我想去接西顿。在import counter

cdef public void increment():

counter.increment()

cdef public int get():

return counter.get()

cdef public void say(int times):

counter.say(times)

这是我用来调用函数的“粘合代码”计数器.py,一个纯Python源代码文件。它的布局是这样的:

^{pr2}$

我已经成功地编译并运行了这个程序。功能正常。然而,当我测试这个程序时发生了一件非常奇怪的事情:int main(int argc, char *argv[]) {

Py_Initialize();

// The following two lines add the current working directory

// to the environment variable `PYTHONPATH`. This allows us

// to import Python modules in this directory.

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append(\".\")");

PyInit_glue();

// Tests

for (int i = 0; i < 10; i++)

{

increment();

}

int x = get();

printf("Incremented %d times\n", x);

printf("The binary representation of the number 42 is");

say(3);

Py_Finalize();

return 0;

}

我希望程序产生以下输出:Incremented 10 times

The binary representation of the number 42 is

101010

但是,它打印的是:Incremented 10 times

101010

The binary representation of the number 42 is

但如果我换线printf("The binary representation of the number 42 is");

到printf("The binary representation of the number 42 is\n");

然后对输出进行校正。在

这在我看来很奇怪。我知道,如果我想打印Python函数的输出,我可以将其返回到C并将其存储在一个变量中,并使用C的printf()而不是本机Python print()。但我很想知道发生这种事的原因。毕竟,printf()语句是在say()语句之前到达的(我在gdb中再次检查了这个语句以确保)。谢谢你的阅读。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值