function
<cstdlib>

system

int system (const char* command);
Execute system command
Invokes the command processor to execute a command.

If command is a null pointer, the function only checks whether a command processor is available through this function, without invoking any command.

The effects of invoking a command depend on the system and library implementation, and may cause a program to behave in a non-standard manner or to terminate.

Parameters

command
C-string containing the system command to be executed.
Or, alternatively, a null pointer, to check for a command processor.

Return Value

If command is a null pointer, the function returns a non-zero value in case a command processor is available and a zero value if it is not.

If command is not a null pointer, the value returned depends on the system and library implementations, but it is generally expected to be the status code returned by the called command, if supported.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* system example : DIR */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* system, NULL, EXIT_FAILURE */

int main ()
{
  int i;
  printf ("Checking if processor is available...");
  if (system(NULL)) puts ("Ok");
    else exit (EXIT_FAILURE);
  printf ("Executing command DIR...\n");
  i=system ("dir");
  printf ("The value returned was: %d.\n",i);
  return 0;
}

Data races

The function accesses the array pointed by command.
Concurrently calling this function with a null pointer as argument is safe. Otherwise, it depends on the system and library implementation.

Exceptions (C++)

No-throw guarantee: this function does not throw exceptions.

If command is not a null pointer, it causes undefined behavior.

See also