1.13.1.  main, and Command-Line Arguments

[ fromfile: clargs.xml id: clargs ]

int main(int argc, char* argv[])
int main(int argCount, char ** argValues)
int main(int argc, char * const argv[])

Example 1.19. src/clargs/clargs-iostream/clargs.cpp

#include <iostream>
#include <cstdlib>  1

int main (int argc, char* argv[]) {
    using namespace std;
    cout << "argc = " << argc << endl;
    for (int i = 0; i < argc; ++i) {
        cout << "argv# " << i << " is " << argv[i] << endl;
    }
    int num = atoi(argv[argc - 1]);
    cout << num * 2 << endl;
    return 0;
}

1

for atoi()


[Note]Note

Try not to confuse this interpretation of 0 with the bool value false, which is also equal to zero.

clargs> ./clargs spam eggs "space wars" 123
argc = 5
argv# 0 is ./clargs
argv# 1 is spam
argv# 2 is eggs
argv# 3 is space wars
argv# 4 is 123
246

Processing Command-Line Arguments with Qt

Example 1.20. src/clargs/qt/clargs.cpp

#include <QTextStream>
#include <QCoreApplication>
#include <QStringList>

int main (int argc, char* argv[]) {
    QCoreApplication app(argc, argv);
    QTextStream cout(stdout);
    QStringList arglst = app.arguments();
    cout << "argc = " << argc << endl;
    for (int i=0; i<arglst.size(); ++i) {
        cout << QString("argv#%1 is %2").arg(i).arg(arglst[i]) << endl;
    }
    int num = arglst[argc - 1].toInt();
    cout << num * 2 << endl;
}




[9] Applications that use only types such as QString, QStringList, and QTextStream do not need a QCoreApplication.