Capturing a Program’s Return Value

At the end of the main() function, a return statement must surrender an integer value to the operating system. Does a higher value please the OS? Is it offended by zero? What exactly happens with the value returned when your program is done?

Frankly, the operating system doesn’t give a rip about the return value. The value is retained by the shell, stored as a temporary variable until another program deposits its return value.

For shell script programming in Linux, or batch file programming in Windows, you can access the return value. In Windows, the return value is stored in the batch file variable, ERRORLEVEL. In Unix/Linux, the return value is accessible through the shell wildcard $?. Within a program, however, the two operating systems differ in how they can access the return value from a spawned or child program.

To start with, I present the following code:

2022_02_05-Lesson-a.c

#include <stdio.h>

int main()
{
    puts("I return the value 12 to the operating system");

    return(12);
}

This program has one purpose, to return the value 12 to the operating system. The reason for such silliness is to see how another program running twelve as a spawned or child process detects this return value.

If you run the program in Code::Blocks, the return value is present in the command window output:

I return the value 12 to the operating system
Process returned 12 (0xC)   execution time : 0.00 s
Press any key to continue.

In Linux, run the program at the command prompt and then use the echo command to confirm the return value:

$ echo $?
12

At the command prompt in Windows, you must code a batch file to fish out the return value. It’s a complex task I don’t need to get into in this Lesson. But if you’re curious, dig up one of my ancient books on MS-DOS Batch File Programming, available in dustbins around the planet.

But what happens with the return value when one program spawns another? Can the first program retrieve the value?

Back when I was coding in DOS, I used the system() for one program to run another. It accepts an argument as a string you would type at the command prompt. The return value from system() is the program’s return value. This approach doesn’t work in Linux.

The following code runs the twelve program, which is created from the source code shown earlier.

2022_02_05-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int r;

    r = system("./twelve");
    printf("Child process exit status = %d\n",r);

    return(0);
}

In Windows, the system() function (Line 8) must be changed to read: system("twelve")

Assuming that the twelve program exists in the same directory where you run the above code’s program, here is the output in Windows:

I return the value 12 to the operating system
Child process exit status = 12

And here is the output from Linux:

I return the value 12 to the operating system
Child process exit status = 3072

The value you see in Windows is the program’s actual return value. The value appearing in Linux is the return value of the shell — not the program run.

It’s possible to obtain a child program’s return value in Linux. But to do so you must do more than use the system() function. I cover this topic in next week’s Lesson.

Leave a Reply