Question

I am trying to test whether a c program has compiled and executed correctly or not.

Say I have just print Hello World in a c program so I want to write a c program to check

that the first program has returned 0 or has returned something else.

How can i do that.

Thanks in advance.

Was it helpful?

Solution

This is usually platform-dependent and depends on how you run one program from another. If you use the C library function system, you can run the program and then read the status code from that program as follows:

int returnCode = system("./hello-world-program");
if (returnCode == 0) {
     ...
}

However, you're usually better off using OS-level primitives to do this. Linux uses fork and exec to handle this, and you can read the child process's exit code given the process ID number by using the wait function in conjunction with some other code. Windows has its own mechanism for doing this which, unfortunately, I'm not familiar with.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top