Walker News

How To Execute Operating System Command In C Program?

The stdlib.h header file contains definition of system() function, which you can use to run a system command from within program written in C Language.

However, not all C implementations include a command processor for executing other program. That means, if the return value of system(NULL) is ZERO, then your C program is not capable to execute any system command :(

Here is the sample code (runLinuxCmd.c) that will execute any command specified in the program argument (command-line option of the C program):
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
  if (system(NULL))
    puts("No command processor!");

  return 0;

Run g++ -o runLinuxCmd runLinuxCmd.c to compile it into binary progam file. Next, let’s test it. For example, get it to run the GNU date command:
runLinuxCmd "date +%s"

Custom Search

2018  •  Privacy Policy