Command-line arguments: Difference between revisions

Content added Content deleted
m (→‎{{header|C++}}: Minor formatting changes)
Line 617: Line 617:
Command line arguments are passed the same way as in C.
Command line arguments are passed the same way as in C.


This example uses iostream. Traditional C I/O also works.
This example uses <code><iostream></code>. C-style i/o also works.


<syntaxhighlight lang="cpp">#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


int main(int argc, char* argv[])
int main(int argc, const char* argv[]) {
std::cout << "This program is named " << argv[0] << '\n'
{
std::cout << "This program is named " << argv[0] << std::endl;
<< "There are " << argc - 1 << " arguments given.\n";
for (int i = 1; i < argc; ++i)
std::cout << "There are " << argc-1 << " arguments given." << std::endl;
std::cout << "The argument #" << i << " is " << argv[i] << '\n';
for (int i = 1; i < argc; ++i)
std::cout << "the argument #" << i << " is " << argv[i] << std::endl;

return 0;
}</syntaxhighlight>
}</syntaxhighlight>