This blog is under construction

Monday 30 April 2012

system example in C

Header file:
    stdlib.h

Synopsis:
     int system(const char *cmd);

Description:
       It executes the command mentioned in the string cmd by calling /bin/sh -c cmd


system function C example:


  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  int main() {
        char cmd[100];
        printf("Enter your command:\n");
        fgets(cmd, 90, stdin);
        cmd[strlen(cmd) - 1] = '\0';
        printf("Output for the given command:\n");
        system(cmd);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/stdlib$ ./a.out
  Enter your command:
  ls -a  a*
  Output for the given command:
  abort.c  abs.c    a.out  atexit.c  atof.c  atoi.c  atol.c


No comments:

Post a Comment