System c library – system C Library Function

System c library: The function system transfers the command name specified by command to the host environment to be executed by the command processor. It returns after the command has been completed in host environment.

Function prototype of system

int system(const char *command);
  • command : This is a null terminated string containing the system command to be executed.

Return value of system

System function in c: If command is not NULL, then function returns the status code of the command otherwise -1 in case of error.
If command is NULL, then function returns a non-zero value in case a command processor is available otherwise zero.

C program using system function

System function in c: The following program shows the use of system function to run system commands in host environment.

system C Library Function

#include <stdio.h>
#include <string.h>
 
int main () {
   char command[100];
    
   printf("Enter any system command as string\n");
   gets(command);
    
   system(command);
   getch();
}

Output

Enter any system command as string
dir
system.c
system.exe