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相關知識,請關注IT培訓機構:千鋒教育。