int s = system("./my_prog 32");
有效,但如何将参数作为变量引入? int a = 32; int s = ("system %d", a);
似乎不起作用(“函数‘system’的参数太多”。)
最佳答案
C 中的 system() 函数接受一个 const char *
类型的参数。这就是您的第一个示例有效的原因(尽管您的第二个示例格式不正确)。
仍然可以使用 stdio.h
中的 sprintf()
函数实现您想要的。 int a = 32;字符命令[80]; sprintf(command, "./my_prog %d", a);系统(命令);
关于c - 如何从 C 程序中执行带参数的可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50634351/