Mal Mal - 26 days ago 19
C Question

How to run a C program in bash script and give it some values of a defined constant?

I have a program in C (bact.c), where I define a constant value N.

I would then like to run the code for different values of N,
not consecutively (e.g. N=100,120,300,350,426...).

How can I automate the process with a script, which loops over the
choosen values N? Is it possible without scanf?

Thank you very much in advance.

Answer

This would only work if your program accepts the value of N as a command-line argument.

If you call your program as ./a.out 3 where 3 is the value for N, then you could write a one-liner in the terminal as:

for N in {100,120,300,350,426}; do ./a.out $N; done

You can even make a script out of it if you like.