The document explains how to pass command line arguments in C and C++ programs using the main function's parameters argc and argv. It provides an example of a program that prints each argument passed to it, along with the rules governing command line arguments. Key points include that argc represents the number of arguments and argv is an array of character strings containing the arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views4 pages
Command Line Arguments
The document explains how to pass command line arguments in C and C++ programs using the main function's parameters argc and argv. It provides an example of a program that prints each argument passed to it, along with the rules governing command line arguments. Key points include that argc represents the number of arguments and argv is an array of character strings containing the arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Command Line Arguments
When executing a program in either C or C++ there is a
way to pass command line arguments. Eg. >sum 10 20 Passed a character arrays. Each parameter separated by a space Comes into the program as two arguments argc – Number of parameters argv – Parameter list Command Line Arguments #include <iostream> using namespace std;
int main(int argc, char *argv[])
{ for (int i=0; i<argc; i++) printf(“This is Argument number %d --> %s \n”, i, argv[i]); return 0; } Sample Output >hello Each word should be a unique argument This is Argument number 0 -->hello This is Argument number 1 -->Each This is Argument number 2 -->word This is Argument number 3 -->should This is Argument number 4 -->be This is Argument number 5 -->a This is Argument number 6 -->unique This is Argument number 7 -->argument Command Line Arguments Conventional rules:
Arguments are always passed to main( ).
There must be two first is an integer second char pointer to an array First argument (argv[0]) will always be the name of the calling program. argc will always be at least 1 The first argument is always argv[0] The last argument is always argv[argc-1] argv[argc] will always be a null pointer Arguments are always passed as character strings. Numbers must be converted from characters to integers, floats, doubles, etc.