Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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
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
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
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:
Argument
Use
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: