是否可以编写一个脚本来针对不同的 A 值运行此代码;
#include <stdio.h>
#define A 3
int main (){
printf("In this version A = %d\n", A);
return(0);
}
我猜是类似 for 循环的东西?
最佳答案
Is it possible to write a script to run this code for different values of A;
并非如此,因为宏 A
在代码中定义了固定值。相反,您可以将该值作为参数传递:
#include <stdio.h>
int main(int argc, char **argv){
if(argc == 2) {
printf("In this version A = %s\n", argv[1]);
}
return 0;
}
(代码不会检查其输入是否为整数——您可以根据需要进行测试)。
您可以通过脚本运行它。例如,使用 bash 的 for
循环编译上面的代码 (gcc -Wall -Wextra test.c -o test
):
$ for ((i = 0; i < 10; i++)); do ./test $i; done
In this version A = 0
In this version A = 1
In this version A = 2
In this version A = 3
In this version A = 4
In this version A = 5
In this version A = 6
In this version A = 7
In this version A = 8
In this version A = 9
$
关于使用 Shell 脚本更改 C 程序中的某些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792172/