Table of contents
1.
Introduction
2.
What is system() Function?
2.1.
Advantages of using system() Function
2.2.
Disadvantages of using system() Function
3.
Implementation of system() Function
3.1.
Checking for the Command Processor
3.2.
Using color Command with system()
3.3.
Using ls Command with system()
3.4.
Using dir Command with system()
4.
System() Function Arguments
5.
Frequently Asked Questions
5.1.
How to print the directories with the system() function?
5.2.
In which operating systems, the system() function can be used?
5.3.
Is there any alternative for the system() function?
5.4.
How to retrieve the current date using the system() function?
6.
Conclusion
Last Updated: Dec 23, 2024
Easy

System() Function in C/C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Do you also have a question on how can we interact with the operating system by a programming language? 

So, there is a function known as system() that can do this for us. system() is the function of the C/C++ standard library, which can be used to call the operating system commands with your program.

system() function in c/c++

In this article, we will take a look at the System() Function in C/C++, where we will discuss what is system() function is, how to use it, and the implementation of the system() function using various examples.

Read about, Introduction to Functions.

What is system() Function?

In C or C++, there is a function called system(), which is used to call that executes the operating system commands on the terminal. The terminal can be anyone, such as Windows command prompt, VS Code command prompt, or PowerShell. 

The system() function can be used by importing a library called <stdlib.h>.

Here is a syntax for using the system() function:

int system(char command)

Advantages of using system() Function

  • The system() is a function that is reusable and can be used multiple times.
     
  • Using the system() function is very easy and simple to use if you know how to use functions in C/C++.
     
  • The system() function directly interacts with the kernel of the operating system. So you have control over your operating system.

Disadvantages of using system() Function

  • Calling a system() function is very heavy and expensive, which takes a lot of resource consumption.
     
  • system() function can only be used on systems that have the pause command on the system level, such as Windows and DOS.

Implementation of system() Function

In this section, we will be going to see multiple examples of using the system() function in C/C++ as follows:

Checking for the Command Processor

Before directly jumping into the example, we should first check if the command processor is installed on our systems or not. For which we can call the system() function by passing nullptr (or NULL) as an argument. If this function returns true means the command processor is present otherwise, no. 

If your output returns to be false, then you should consider installing the compiler from scratch because the system() function is a part of the standard library. You can download the compiler from the official documentation here.

Here is an implementation code to check:

#include <iostream>
#include<stdlib.h>
using namespace std;

int main() {

/* Checking if the system() return true 
means the command process is present; otherwise no */
   
   if(system(nullptr))
        cout<<"Yes Command Processor is present";
   else
        cout<<"No Command Processor is not present";
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

output 1

Using color Command with system()

The system() function can be used to change the color of the text of Windows command prompt. The “color” can be passed to the system() function as an argument; here is the implementation below:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

/* Calling the system() function with “color” 
as an argument with color code as b (blue). */
   system("color b");
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output 2

Using ls Command with system()

The system() function can be used to print the list of all the files in the currently opened folder on the command prompt by passing “ls” as the argument that will retrieve the list of the files in the currently opened folder; here is the implementation below:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

/* Calling the system() function
 with “ls” as an argument. */
   system("ls");
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

output 3

Using dir Command with system()

The system() function can be used to print the list of all the directories in the currently opened folder on the command prompt by passing “dir” as the argument; here is the implementation below:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

/* Calling the system() function with “dir” as an argument. */
   system("dir");
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output 4

System() Function Arguments

In this section, we will look at some arguments which you can pass to your system() function to interact with your operating system with the help of this below table:

ArgumentUse

color

The “color” can be used to change the text color of the command prompt

ls

The “ls” can be used to list all the files in the current directory.

dir

The “dir” can be used to list all the directories in the currently opened folder.

pause

The “pause” is used to wait for the user to input

date

The “date” argument can be used to get the current date of your system.

kill

The “kill” can be used to kill the running process.

Frequently Asked Questions

How to print the directories with the system() function?

We can use the system() function to print the list of directories on the command prompt. We need to pass an argument called “dir” in the system() function, and the syntax will be system(“dir”);

In which operating systems, the system() function can be used?

The system() can not be used in some systems that have the pause command on the system level, like Windows and DOS, and not Linux.

Is there any alternative for the system() function?

There are multiple functions that you can use instead of system(). You can use functions such as execve(), execv(), and execl() to execute the commands for the interaction with the operating system.

How to retrieve the current date using the system() function?

We can use the system() function to print the current data on the command prompt. We need to pass an argument called “date” in the system() function, and the syntax will be system(“date”);

Conclusion

If you want to interact with the operating system, the system() function can be used for the interaction using the C/C++ program. In this article, we discuss what this function is, the syntax of the system() function, and how to use it with different examples.

Here are more articles that are recommended to read:

 

Live masterclass