Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - system()

C Library Function - system()

C Standard Library - <stdlib.h> C Standard Library -

Description

C library function *int system(const char command) Puts the command passes the specified command name or program name to the host environment to be executed by the command processor, and returns after the command completes.

Declaration

Below is the declaration of the system() function.


int system(const char *command)

Parameters

Return Value

If an error occurs, the return value is -1; otherwise, it returns the status of the command.

Example

The following example demonstrates the use of the system() function, listing all files and directories in the current directory on a unix machine.

Example

#include

<
stdio.h
#include
<
string.h
#include
<
stdlib.h
int
main
(
)
{
char
command
[
50
]

;

strcpy

(

command

,

"

ls -l

"

)

;

system

(

command

)

;

return

(

0

)

;

}

Let's compile and run the above program; on a unix machine it will produce the following result:


drwxr-xr-x 2 apache apache 4096 Aug 22 07:25 hsperfdata_apache
drwxr-xr-x 2 railo railo 4096 Aug 21 18:48 hsperfdata_railo
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_XXGLOBAL_1
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_asp_2
srwx---- 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp
rw------ 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp_1280495620
srwx---- 1 apache apache 0 Aug 21 18:48 mod_mono_server_global

The following example demonstrates the use of the system() function, listing all files and directories in the current directory on a windows machine.


#include 
#include 

int main ()
{
   char command[50];

   strcpy( command, "dir" );
   system(command);

   return(0);
} 

Let's compile and run the above program; on a windows machine it will produce the following result:


a.txt
amit.doc
sachin
saurav
file.c

C Standard Library - <stdlib.h> C Standard Library -