Everything you need to know about system pause c++ function

What is a system() function?

system() is a predefined standard C/C++ library function. You can pass input commands such as “date” or “pause” to the system() function which will be executed on the operating system terminal.

Let’s first understand the syntax of the system() function

int system(const char *string);

int: int is the return type of system() function. If the command is executed successfully without any error returns 0 (zero). If any error occurs during function execution then it returns a non-zero value.

system: it is the name of a function.

Const char: You can pass any defined commands as a string to this function.

For example:

system(“date”): It returns and displays the current date from the system.

system(“mkdir  NewDirectory”): It creates a new folder with the name NewDirectory where the program is available

system(“cd”): It returns and displays the current directory path.

What is the use of a system(“pause”) function?

system pause c++ function

the system (“pause”) function is used to pause the program at any required stage or at the end of the program to check the output of the program on the console terminal.

Some IDE’s wait for the user’s inputs before closing the console window but in some IDE’s, it’s required to add a pause function explicitly so that the console window will wait for the user’s input before closing it.

system pause c++ function is generally used if the user wants to see the output results on the console window. It helps users to debug the program in a better way and users can see output values at different stages of the program.

Required header file to use system(“pause”) function:

To use the system function in your program, you must include the following header file at the beginning of the program.

#include <stdlib.h>

Example of using system(“pause”) function:

#include<iostream>
#include <stdlib.h>
using namespace std;
 
int main()
{
    int storedValues[5] = { 10, 20, 30, 40, 50 };
    
    for (int iCnt=0; iCnt<5; iCnt++) 
    { 
        if ( storedValues[iCnt]== 40) 
	{       
            cout << "Value 40 is available at array position: " << iCnt;
            // pause program
	    system("pause");
        }
    }
}

Disadvantages of the system(“pause”) function:

Dependent on the operating system:

Not all operating system supports system() function. It’s only supported on Windows or DOS operating systems. Linux and other operating systems do not support this function. If you want that your program can run on any operating system then using this function will give you errors on unsupported operating systems.

Resource Heavy function:

This is an operating system-level function. When you call this function, it passes commands to the operating system to pause the execution of the program. To simply pause the program execution, calling an operating system level function is not at all recommended. It’s like calling THOR to fit the fastener in the wall. If we can use any program-level function then that would be great.

Unnecessary header files:

To use the system function, you must include the stdlib.h or cstdlib.h header files at beginning of your program.

If you are not using any other functions from these header files apart from the system function then it’s unnecessary to add an entire header file just for a simple task. It will import all the functions available in these header files. It will unnecessarily increase the size of the dependent libraries.

Impacts program performance:

As it calls the operating system to execute the command and adds the entire header file for a simple function, it definitely impacts program performance. If the performance of a program is critical then you should not use this function. There are cheaper and easy alternate solutions are available to achieve the requirement. Keep reading.

Alternatives of the system(“pause”) function:

There are the following better alternatives available rather than using the system pause c++ function.

Use cin.get() function:

cin.get() is the best alternative for the system(“pause”) function. It will also stop the program execution at the required location. You can use cin.get() function as shown below.

#include<iostream>
using namespace std;
 
int main()
{
    int storedValues[5] = { 10, 20, 30, 40, 50 };
    
    for (int iCnt=0; iCnt<5; iCnt++) 
	{ 
        if ( storedValues[iCnt]== 40) 
		{       
            cout << "Value 40 is available at array position: " << iCnt;
            // pause the program to read output
	    cin.get();
        }
    }
}

cin.get() function stops the program execution and waits for input from the user before executing further. Once the user enters any value then program execution continues. This function can be useful if you want to pass any value to the program during execution.

cin.get() is a program-level function, it will not invoke the operating system to execute the command. system pause c++ is a standard library function hence there is no need to add a separate header file explicitly.

Note:  If you are using the C programing language then use getch() function to pause the program execution.

Add breakpoints in the code:

Adding a breakpoint in the program for debugging is the more preferred and efficient way to evaluate the program execution. If you just want to just debug the output of your program then you can use breakpoints. But this is possible only if you are using any IDE to debug the code. If you are not using any IDE and executing the code from the command line then the first approach is useful.

Also read…

How to call a java jar file from a C++ program using system() function.

Final thoughts:

So here we have understood the details about system function and more importantly about system pause c++ function. We hope this article was useful and you got clear about why we should not use the system(“pause”) function. Please let us know your experience and suggestions in the comment box or you can reach out to us using the contact form. We would be happy to hear from you.

Leave a Comment