使用 Shell 脚本更改 C 程序中的某些内容

标签 c linux bash shell unix

是否可以编写一个脚本来针对不同的 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/

相关文章:

linux -/usr/sbin/sendmail 发送带有 .html 附件的邮件

linux - 在 linux bash 中有一种方法可以在不使用 xte 或其他类似程序的情况下模拟 y 按键

bash - 使环境变量可用于脚本的万无一失的方法是什么?

bash - 如果找不到匹配项,则通过 grep 返回新行

c - 当我尝试从 C 中 unsafe_load 一个指针时,Julia 得到了错误的值

c - 如何从文件描述符获取文件名和路径?

C 编程段错误 : 11 Thread issues

linux - 在 Linux 上从 64 位代码远程调用 __USER32_CS

c - 我试图解决的堆栈实现示例

c - syscall读写是否适用于System V消息队列发送和接收消息