C Library Functions -system()

C Standard Library - <stdlib.h>

Description

C Library Functionsint system(const char *command)holdcommandThe specified command name or program name is passed to the host environment to be executed by the command processor, and returns after the command completes.

Declaration

The following is the declaration of the system() function.

int system(const char *command)

Parameter

  • command-- A C string containing the name of the requested variable.

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 usage 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 us compile and run the above program, which will produce the following results on a Unix machine:

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 usage of the system() function, listing all files and directories in the current directory on a Windows machine.

#include <stdio.h>
#include <string.h>

int main ()
{
   char command[50];

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

   return(0);
} 

Let us compile and run the above program, which will produce the following results on a Windows machine:

a.txt
amit.doc
sachin
saurav
file.c

C Standard Library - <stdlib.h>

other extensions