我需要多次运行我的代码,更改头文件中定义的变量。
我想编写一个 bash 脚本来编译(通过 makefile)并运行
./myprogram some_value
Run Code Online (Sandbox Code Playgroud)
有没有办法使用我从 atof(argv[1]) 中恢复的内容,我包含在 myprogram.c 中?(在许多其他文件之间共享的标头,包含我在代码中使用的参数的值)
在那个标题中,我有类似的东西
float the_parameter;
Run Code Online (Sandbox Code Playgroud)
我想初始化为 atof(argv[1])
首先,extern在头文件中添加变量声明。
extern float the_parameter;
Run Code Online (Sandbox Code Playgroud)
然后,在您的源文件之一中,声明变量extern并对其进行初始化。
#include <stdlib.h>
float the_parameter;
int main(int argc, char* argv[]) {
if (argc > 1) the_parameter = atof(argv[1]);
/* other code */
}
Run Code Online (Sandbox Code Playgroud)