Every execution of a program happens inside a process. Your code can spawn another process, which will become its child process, and can wait on it to complete execution and get back the return value. I would suggest reading more on processes. How you achieve this is using the fork() system call. It makes two copies of the process and returns different values in both copies so you can distinguish which process is which. It returns 0 in the child process and process id of the child process in the parent process. Then in the child process you can use one of the system calls from the exec family like execlp(all the functions in exec family differ in the arguments you provide to them, I would suggest reading manpages) to execute your second program. In the parent process, you can wait on the child process to finish execution using the wait() system call and then get back it's return value in a variable. The below code snippet should be helpful
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t childPid = fork();
if (childPid == -1) {
// Handle fork error
} else if (childPid == 0) {
// Code for the child process
execlp("program", "program", arg1, arg2, ..., (char*) NULL);
//Program here is the path to the program you want to run
} else {
// Code for the parent process
int status;
wait(&status);
if (WIFEXITED(status)) {
int returnValue = WEXITSTATUS(status);
// Use the returnValue as needed
} else {
// Child process exited abnormally
}
}
return 0;
}