n.derv n.derv - 10 days ago 6
C Question

Change something in C program with Shell Script

Is it possible to write a script to run this code for different values of A;

#include <stdio.h>
#define A 3

int main (){

printf("In this version A = %d\n", A);
return(0);
}


I guess something like for loop?

Answer

Is it possible to write a script to run this code for different values of A;

Not as it is because the macro A has a fixed value defined in your code. Instead you can pass the value as an argument:

#include <stdio.h>

int main(int argc, char **argv){
    if(argc == 2) {
        printf("In this version A = %s\n", argv[1]);
    }
    return 0;
}

(The code doesn't check if its input is an integer -- which you can test if necessary).

and you can run it via script. For example, compile the above (gcc -Wall -Wextra test.c -o test) using a for loop of bash:

$ 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
$
Comments