The system function is used to request that a command be executed by the operating system. The command is just a string that can be interpreted by the operating system command executed. On UNIX systems this is typically the standard shell, such as /bin/sh. On windows machines this is typically the cmd.exe program.

The system function will send the command string and wait until the command has finished executing. The integer return value indicates the exit status of the program.

The system function requires only one argument. The argument is a NULL terminated string that specifies the exact command that will be executed by the operating system.

The system function can also be used to determine if the command processor is available or not. To do this, use a NULL argument. The return value indicates if the command processor is available (non-zero value) or if it could not be found (in this case zero is returned).

The following code shows an example of the system function in use:

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

int main()
        int res = system(NULL);
        if (res) {
                printf("the command processor is available\n");
        } else {
                printf("the command processor is not available\n");
        res = system("ls");
        printf("the program returned value %d\n", res);
        return 0;
Article created on 2008-08-19 22:23:36

Post a comment