python怎么在c中运行程序

C语言中运行python程序

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

举例如下,Python代码如下

#!/usr/bin/envpython

#test.py

importsys

x=int(sys.argv[1])

printx*x

C语言代码如下

/*test.c*/

#include

#include

intmain()

{

FILE*f;

chars[1024];

intret;

f=popen("./test.py99","r");

while((ret=fread(s,1,1024,f))>0){

fwrite(s,1,ret,stdout);

}

fclose(f);

return0;

}

测试如下

$gcctest.c

$./a.out

9801

以上内容为大家介绍了python培训之怎么在c中运行程序,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

推荐阅读