Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

System() Function in C/C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

 

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;
}


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;
}

 

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;
}


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:

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:


You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMS, and System Design, etc. as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts.

Happy Learning!

Topics covered
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