for example, if you want the output of ls command you can use the following code.
but...first of all: this is a dirty solution: if your program must does other things, you will fork the process and in the child use execl.
then: in my solution the output (for semplicity) is sent to a file (argv[1]) but you can throw it wherever you want once you put it in a file.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, const char* argv[]) {
int fd = open(argv[1],O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
dup2(fd,STDOUT_FILENO);
execlp("ls","ls",0);
return 0;
}