python怎么在c中运行程序

469次阅读
没有评论

python怎么在c中运行程序

C语言中运行python程序

C语言使用popen/system或者直接以系统调用级fork+exec来运行python程序也是一种混编的手段了。

举例如下,Python代码如下

#!/usr/bin/env python
# test.py
import sys
x = int(sys.argv[1])
print x*x

C语言代码如下

/* test.c */
#include <stdio.h>
#include <stdlib.h>
int main()
{
        FILE *f;
        char s[1024];
        int ret;

        f = popen("./test.py 99", "r");
        while((ret=fread(s,1,1024,f))>0) {
                fwrite(s,1,ret,stdout);
        }
        fclose(f);
        return 0;
}

测试如下

$ gcc test.c
$ ./a.out
9801
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2019-11-09发表,共计470字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)