Latest web development tutorials

C library functions - system ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionsint system (const char * command) the command name or the name of the program specified commandcommand passed to the host environment to be executed by a processor, and returns after the command completes.

statement

Here is the () function declaration system.

int system(const char *command)

parameter

  • command - the name of the variable that contains the requested C string.

return value

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

Examples

The following example demonstrates the system () function is used, a list of all files and directories under the current directory on unix machines.

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

int main ()
{
   char command[50];

   strcpy( command, "ls -l" );
   system(command);

   return(0);
} 

Let's compile and run the above program, the following results will be in 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 system () function is used, a list of all files and directories under 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's compile and run the above program, the following results in windows machine will:

a.txt
amit.doc
sachin
saurav
file.c

C standard library - <stdlib.h> C standard library - <stdlib.h>