C語(yǔ)言中運(yùn)行python程序
C語(yǔ)言使用popen/system或者直接以系統(tǒng)調(diào)用級(jí)fork+exec來(lái)運(yùn)行python程序也是一種混編的手段了。
舉例如下,Python代碼如下
#!/usr/bin/envpython
#test.py
importsys
x=int(sys.argv[1])
printx*x
C語(yǔ)言代碼如下
/*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;
}
測(cè)試如下
$gcctest.c
$./a.out
9801
以上內(nèi)容為大家介紹了python培訓(xùn)之怎么在c中運(yùn)行程序,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。