Q:

Running 'wc' using execvp() recognizes /home/usr/foo.txt but not ~/foo.txt

I have a program where I want to run the command wc using execvp(). 
The important bits are here:
char *argvNew[5];
argvNew[0] = "wc";
argvNew[1] = "/home/user/foo.txt";
argvNew[2] = NULL;
execvp(argvNew[0], arvgNew);

This works just fine and the terminal displays the output from wc. However, when I change the line to: 
argvNew[1] = "~/foo.txt"

I get this error in the terminal:
wc: '~/foo.txt': No such file or directory

Running wc ~/foo.txt directly from the terminal behaves exactly as running wc /home/user/foo.txt. Why does execvp have an issue with the ~/ part?

A:

Tilde (~) expansion is done/understood by shells only - it's not expanded in C programs. So execvp attempts to interpret ~ as part of the filename (argument to wc).
What you can do is get the HOME directory value using getenv and prefix it with the filename (e.g., using snprintf).

