Question:
C++ question on command line arguments?
?
2010-12-03 23:05:31 UTC
Hi ,
I had a few questions on command line arguments:

1) What is the value of argc if the command line arguments to run your program are:
a.out input.dat output.dat and the main function header is the following:
int main( int argc, char * argv[ ] )
The answer is 3 but can someone explain to me why?


2) What is the value of argv[0] given the same command line argument and main function header as the
previous question?
The answer is "a.out", but can you explain why? I dont get it.

P.S. the 2nd question refers to the first


THanks
Four answers:
gene_frequency
2010-12-03 23:51:00 UTC
1.) argc == 3 because (presumably) these are the 3 items (arguments) on the command line:

a.out

input.dat

output.dat



2.) argv[0] always points to the program name.



There's a little more to it. And more words might confuse you more. Here is a nice example you could compile and work with:



int main(int argc, char *argv[])

{

int iii;



cout << "Arguments on the command line: " << argc <<". They are:\n";

for(iii=0; iii < argc; iii++)

cout << argv[iii] << endl;

return 0;

}



Here is some actual run time out put -- copy and pasted:

---------- snip -------------

C:\PROJECTS\_HELP>help.exe hello world -- what's up?

Arguments on the command line: 6. They are:

help.exe

hello

world

--

what's

up?

---------- snip -------------
?
2010-12-03 23:41:57 UTC
1) argc contains the number of command-line arguments. So, since the program was called with three command-line arguments a.out, input.dat and output.dat, the value of argc is 3. If you included an additional command-line argument (say, something like "numbers.txt"), then argc would be equal to 4.



2) argv is a cstring array containing the actual command-line arguments. So, calling the program with the three arguments listed above,



argv[0] is "a.out"

argv[1] is "input.dat", and

argv[2] is "output.dat"
moton
2016-10-19 11:31:34 UTC
Lawrence's code is right, yet whilst all you are going to do is reproduction the counsel from the argv array, you are able to in basic terms go away them interior the array. even nevertheless, copying the counsel that would desire to descriptively named variables is solid for clarity. yet you look perplexed approximately copying the contents of an array vs. copying a pointer to the array. this could be what you have in strategies: #incorporate #incorporate #define NAME_LEN 15 int significant(int argc, char *argv[]) {     char firstname[NAME_LEN+a million];     char lastname[NAME_LEN+a million];   if (argc > 2) {       strncpy(firstname, argv[a million], NAME_LEN);       strncpy(lastname, argv[2], NAME_LEN);       printf("the 1st call is: %s. The final call is: %sn", firstname, lastname);   } else {       printf("utilization: %s n", argv[0]);       return -a million;   }   return 0; } #if 0 pattern run: $ ./args bill ok. the 1st call is: bill. The final call is: ok. #endif
Otherworld
2010-12-03 23:31:00 UTC
To explain what the answer is the code of the program has to be presented. Otherwise, we cannot tell why the answer is 3 or "a.out".

If you want, you can post your code and contact me for help


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...