vimwiki

C++ command line arguments

Passing command line arguments

Pass them after the executable name

g++ greeting.cpp -o greeting ./greeting commandLineArgument

Note: command line arguments are always passed as string ./C++_program 12 will be passed as "12" in the program.

Using command line arguments

int main(int argc, char* argv[]);
// or
int main(int argc, char** argv);
#include <iostream>

int main(argc, char* argv[]) {
    for(int i = 0; i < argc; i++) {
        std::cout << argv[i];
    }
    return 0;
}