I do not fully understand the concept of argc and argv in co

I do not fully understand the concept of argc and argv in computer science c language.

Solution

Hi, Please find my explanation.

Please let me knwo in case of any issue.


The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed,
and argv[] is a pointer array which points to each argument passed to the program.

int main( int number_of_args, char* list_of_args[] )
{
...
}


//
// Note: Often you will see \"traditional\" programs using the more
// archaic (if more concise) \"Lingo\" and syntax for
// these values (argc and argv)
//

int main( int argc, char** argv )
{
...
}

number_of_args : the total number of \"values\" on the command line.

Note: The name of the program is counted and is the first value.

Note: Values are defined by lists of characters separated by whitespace.

list_of_args : this is an array of strings.

Note: The array has a length defined by the number_of_args parameter.
  

argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied,
and *argv[n] is the last argument.
If no arguments are supplied, argc will be one, and if you pass one argument then argc is set at 2.

You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes \"\" or single quotes \'\'.


Here is a sample program to print out all the information \"given\" to the program:

int main( int number_of_args, char* list_of_args[] )
{
for ( int i=0; i<number_of_args; i++)
{
printf(\"the %2d arg is: %s\ \", i, list_of_args[i]);
}
}
  

I do not fully understand the concept of argc and argv in computer science c language.SolutionHi, Please find my explanation. Please let me knwo in case of any

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site