×

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We need to use <cstdlib> while using the system commands. As there are few commands that are linked with the system function.

There are many commands that can be used with the system keyword and one of them is,

System(“date”)

Example:1

#include <iostream>
using namespace std;
int main(){
//system function is used to print the date
  system("date");
}

Output:

system() Function in C++

Explanation:

In the above code, we have used the system function to print the current date. As we can see, when we use the system(“date”), the then-current date is printed in the output console in the form of dd-mm-yy.

Example-2: Code for changing the color of the cmd

#include <iostream>
using namespace std;
int main(){
  
  system("color a");
}

Explanation:

In the above code, we have used the system function to change the color of the output terminal. By using the “color a, “ the output terminal color will be changed to pink.

There are a few commands that are present in the Linux

#include <iostream>
#include <cstdlib>


using namespace std;


int main() {
    // Try the "ls -l" command from your Linux / Mac machine
    int ret = system("ls -l > test.txt");
    return 0;
}

Output:

system() Function in C++

System(“pause”);

The system (“pause”) will pause the program until we press any key. When the pause statement executes, the os will pause the program.

Example:

#include <iostream>
#include <cstdlib>


using namespace std;


int main() {
    for (int i=0; i<10; i++) {
        cout << "i = " << i << endl;
        if (i == 5) {
            // Call the pause command
            cout << "Calling the pause command\n";
            system("pause");
            cout << "pause program terminated. Resuming...\n";
        }
    }
    return 0;
}

Output:

system() Function in C++

Explanation:

In the above code, we have used the pause command in the system function. We can observe a pause in the for loop, which will be resumed when we press any key then the remaining part of the code will be executed. The two statements Calling the pause command and pause program terminated. Resuming... will be displayed until the key is pressed.


Related Topics

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.