Format
#include <stdlib.h> int system(char *string);
Language Level: ANSI, XPG4, Extension
system passes the command string to a command
processor to be run. The command processor specified in the
COMSPEC environment variable is first searched for. If it does
not exist or is not a valid executable file, the default command
processor, CMD.EXE, is searched for in the current directory and
in all the directories specified in the PATH environment
variable.
If the specified command is the name of an executable file created from a C program, full initialization and termination are performed, including automatic flushing of buffers and closing of files. To pass information across a system function, use a method of interprocess communication like pipes or shared memory.
You can also use system to redirect standard streams using the redirection operators (the angle brackets), for example:
rc = system("cprogram < in.file");
The defaults for the standard streams will be whatever the standard streams are at the point of the system call; for example, if the root program redirects stdout to file.txt, a printf call in a C module invoked by a system call will append to file.txt.
Return Value
If the argument is a null pointer, system returns
nonzero if a command processor exists, and 0 if it does not.
system returns the return value from the command processor if it
is successfully called. If system cannot call the command
processor successfully, the return value is -1 and errno is set
to one of the following values:
| Value | Meaning |
| ENOCMD | No valid command processor found. |
| ENOMEM | Insufficient memory to complete the function. |
| EOS2ERR | A system error occurred. Check _doserrno for the specific error code. |
Example
This example shows how to use system to execute the
command dir c:\.
#include <stdlib.h>
int main(void)
{
int rc;
rc = system("dir c:\\");
return rc;
/* The output should be a listing of the root directory on the c: drive */ }
![]()
exit -- End Program
_exit -- End Program
<process.h>
<stdlib.h>