In C
you can concatenate two string literals, but it doesn't work with variables.
You have to concatenate using e.g. a function from the printf family:
char buffer[256];
snprintf(buffer, sizeof(buffer), "awk '{ sum += $3 } END { print sum }' %s", filename);
system(buffer);
Note, however, that if filename
contains special characters like '
or , it may break the command (it's a security issue, too).
So you have to escape the filename properly.
A better alternative would be to use a function from the exec family:
execlp("awk", "awk", "{ sum += $3 } END { print sum }", filename, NULL);
execlp() takes every separate command argument in a separate function argument, thus freeing you from having to do concatenations and escaping.
The first argument is the program to execute. The second one is the first argument (usually, this is the program's name). Other program arguments follow. NULL indicates the end of the arguments.