Namespaces
Variants

std::system

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Undefined behavior utilities
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
 
 
Defined in header <cstdlib>
int system( const char* command );

Calls the host environment's command processor (e.g., /bin/sh, cmd.exe) with the parameter command. Returns an implementation-defined value (usually the value that the invoked program returns).

If command is a null pointer, checks if the host environment has a command processor and returns a nonzero value if and only if the command processor exists.

Parameters

command - character string identifying the command to be run in the command processor. If a null pointer is given, command processor is checked for existence

Return value

Implementation-defined value. If command is a null pointer, returns a nonzero value if and only if the command processor exists.

Notes

On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG.

The related POSIX function popen makes the output generated by command available to the caller.

On POSIX systems, std::system blocks the calling thread until the child process executing the command interpreter terminates.

Example

#include <chrono>
#include <cstdlib>
#include <iostream>

int main()
{
    if (not std::system(nullptr))
    {
        std::cout << "The command processor not found.\n";
        return EXIT_FAILURE;
    }

    std::cout << "The command processor exists.\n" << std::flush;
    std::system("ls -l"); // executes the UNIX command "ls -l"

    std::cout << "Before system()...\n" << std::flush;

    const auto start{std::chrono::steady_clock::now()};
    std::system("sleep 1"); // executes the UNIX command "sleep 1"
    const auto finish{std::chrono::steady_clock::now()};

    std::cout << "After system()...\n"
                 "Elapsed: " << std::chrono::duration<double>(finish - start) << '\n';

    return EXIT_SUCCESS;
}

Possible output:

The command processor exists.
total 24
-rwxr-xr-x 1 cpp cpp 17184 Nov 15 00:08 example_std_system
-rw-r--r-- 1 cpp cpp   720 Nov 15 00:05 example_std_system.cpp
Before system()...
After system()...
Elapsed: 1.01234s

See also

C documentation for system