0% found this document useful (0 votes)
3 views5 pages

Passing Command-line Arguments to a c++ Program

The document explains how to pass command-line arguments to a C++ program, detailing the syntax for the main() function and the use of argc and argv. It includes a lab activity that demonstrates finding the maximum and minimum numbers from an array, first through standard input and then modified to accept command-line input. The document provides example code for both versions of the program.

Uploaded by

Waqar Ahmad Daha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Passing Command-line Arguments to a c++ Program

The document explains how to pass command-line arguments to a C++ program, detailing the syntax for the main() function and the use of argc and argv. It includes a lab activity that demonstrates finding the maximum and minimum numbers from an array, first through standard input and then modified to accept command-line input. The document provides example code for both versions of the program.

Uploaded by

Waqar Ahmad Daha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Passing command-line arguments to a C++ program

Command line argument is a parameter supplied to the program when it is invoked.


Command line argument is an important concept in C++ programming. It is mostly used
when you need to control your program from outside. Command line arguments are passed
to the main () method.
To pass command line arguments, we typically define main () with two arguments: first
argument counts the number of arguments on the command line and the second is a
pointer array which holds pointers of type char which points to the arguments passed to the
program. The syntax to define the main method is
int main (int argc, char *argv[])
Here, argc variable will hold the number of arguments pass to the program while the argv
will contain pointers to those variables. argv[0] holds the name of the program while argv[1]
to argv[argc] hold the arguments. Command-line arguments are given after the name of the
program in command-line shell of Operating Systems. Each argument separated by a space.
If a space is included in the argument, then it is written in “”.
In the following example, we calculate the average of numbers; passed in the commandline.
Write the following code and save the file named as avg.cpp. Compile the avg.cpp and
create an executable file named avg and execute it. While writing the command to execute
avg pass the numbers whose average is required. It is shown below.
Lab Activities
Activity 1:
Write a program in C++ that find the maximum and minimum number from an array.
Solution:
#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element: " << max;
cout << "Smallest element: " << min;
return 0;
}

Activity 2:
Change the above program such that it accepts the input at command-line.
Solution:
#include<iostream>
#include<cstdlib>
using namespace std;
int main (int c, char *arr[ ])
{
int max = atoi(arr[1]);
for (int i = 0; i < c; i++)
{
int num=atoi(arr[i]);
if (max <num)
max = num;
}
int min = atoi(arr[1]);
for (i = 0; i < c; i++)
{
int num=atoi(arr[i]);
if (min > num)
min = num;
}
cout << "Largest element: " << max<<endl;
cout << "Smallest element: " << min<<endl;
return 0;
}

You might also like