System() Function in C/C++

CC++Server Side ProgrammingProgramming

Given the task is to show the working of system() in C/C++.

The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed.

<stdlib.h> or <cstdlib> should be included to call this function.


The syntax is as follows −

int system(char command)

This function returns zero if the command is executed without any errors.


Input: system(“date”)
Output: The current date is: Fri 12/27/2019

Explanation − The following example shows how we can use the system function to display the current date. The output is shown inside the command processor of the operating system. This shows how we can use the system() function to execute terminal commands or the command that are used inside the command processor.


Input: system(“color a”)

The following example shows how we can use the system() function by simply passing a string into it that contains the command to be executed. Here we have given the “color a” command that is used to change the color of the text in the command processor and here it works properly.

Approach used in the below program as follows

  • First create a variable of type char and give it some suitable size, let’s say cmd[10], so that this string can be used for storing commands.
  • Then use the strcpy() function to store the desired command into the string that we created before. (Do include the <string.h> header file in order to use the strcpy() function)
  • Now pass the string that is storing the command, into the system function.


Step 1-> In function main()
   Declare a char cmd[10]
   Call function strcpy(cmd, “dir”)
   Call function system(cmd)


#include <iostream>
using namespace std;
int main() {
   char cmd[10];


If we run the above code it will generate the following output −

12/25/2019 10:04 AM <DIR> .
12/25/2019 06:33 AM <DIR> ..
12/24/2019 09:56 AM <DIR> bin
12/25/2019 10:04 AM 183 main.cpp
12/25/2019 10:04 AM 1,564,278 main.exe
12/25/2019 06:33 AM 1,046 main.o
12/24/2019 06:33 AM <DIR> obj
12/24/2019 06:33 AM 1,062 Test.cbp
12/24/2019 06:36 AM 358 Test.layout
5 File<s> 1,566,927 bytes
4 Dir<s> 169,866,692,120 bytes free

Here we have given the system function “dir” command as the input, so in return it will show us all the directories present in the computer as the output.

The following method can be used to check if command processor is available in the operating system or not

This can be done by passing a Null pointer into the system() function. If the system returns a non-zero value, then it means that command processor is available.

Else if the system returns zero, then it means that command processor does not exist and the system() function cannot be called.


#include <iostream>
using namespace std;
int main() {
      cout<<”Command Processor is present”;
      cout<<”Command processor is not present”;
   return 0;
Published on 20-Jan-2020 12:13:14