system() in C/C++
system() is used to invoke an operating system command from a C/C++ program.
int system(const char *command);
Note: stdlib.h or cstdlib needs to be included to call system.
Using system(), we can execute any command that can run on terminal if operating system allows. For example, we can call system(“dir”) on Windows and system(“ls”) to list contents of a directory.
Writing a C/C++ program that compiles and runs other program?
We can invoke gcc from our program using system(). See below code written for Linux. We can easily change code to run on windows.
system() vs using library functions:
Some common uses of system() in Windows OS are, system(“pause”) which is used to execute pause command and make the screen/terminal wait for a key press, and system(“cls”) which is used to make the screen/terminal clear.
However, making a call to system command should be avoided due to the following reasons:
- It’s a very expensive and resource heavy function call
- It’s not portable: Using system() makes the program very non-portable i.e. this works only on systems that have the pause command at the system level, like DOS or Windows. But not Linux, MAC OSX and most others.
Let us take a simple C++ code to output Hello World using system(“pause”):
The output of the above program in Windows OS:
Hello World! Press any key to continue…
This program is OS dependent and uses following heavy steps.
- It suspends your program and simultaneously calls the operating system to opens the operating system shell.
- The OS finds the pause and allocate the memory to execute the command.
- It then deallocate the memory, exit the Operating System and resumes the program.
Instead of using the system(“pause”), we can also use the functions that are defined natively in C/C++.
Let us take a simple example to output Hello World with cin.get():
The output of the program is :
Thus, we see that, both system(“pause”) and cin.get() are actually performing a wait for a key to be pressed, but, cin.get() is not OS dependent and neither it follows the above mentioned steps to pause the program.
Similarly, in C language, getchar() can be used to pause the program without printing the message “Press any key to continue…”.
A common way to check if we can run commands using system() in an OS?
If we pass null pointer in place of string for command parameter, system returns nonzero if command processor exists (or system can run). Otherwise returns 0.
Note that the above programs may not work on online compiler as System command is disabled in most of the online compilers including GeeksforGeeks IDE.
This article is contributed by Subhankar Das. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.