Tương tự bài trước, xử lý tham số dòng lệnh qua argc và argv trong main().
Ví dụ
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
if (argc == 2) {
cout << "The argument supplied is " << argv[1] << endl;
} else if (argc > 2) {
cout << "Too many arguments supplied." << endl;
} else {
cout << "One argument expected." << endl;
}
return 0;
}
Kết quả
$./a.out testing The argument supplied is testing
$./a.out testing1 testing2 Too many arguments supplied.
$./a.out One argument expected.