Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:

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

When I code C++ without an IDE, just with a command line compiler, I type:

int main() 

without any parameters. What does this mean, and is it vital to my program?

like image 999
Greg Treleaven Avatar asked Jun 11 '10 15:06

Greg Treleaven


People also ask

What does int argc char * argv [] mean in C?

Command-line Arguments: main( int argc, char *argv[] ) Here argc means argument count and argument vector. The first argument is the number of parameters passed plus one to include the name of the program that was executed to get those process running.

What does char * argv [] mean in C?

The declaration char *argv[] is an array (of undetermined size) of pointers to char , in other words an array of strings. And all arrays decays to pointers, and so you can use an array as a pointer (just like you can use a pointer as an array).

What does argc and argv means?

The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.

What does argv and argc indicate in int main int argc char * argv?

Correct Option: C The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program. The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings.


2 Answers

argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.

The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.

They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.

Try the following program:

#include <iostream>  int main(int argc, char** argv) {     std::cout << "Have " << argc << " arguments:" << std::endl;     for (int i = 0; i < argc; ++i) {         std::cout << argv[i] << std::endl;     } } 

Running it with ./test a1 b2 c3 will output

Have 4 arguments: ./test a1 b2 c3 
like image 98
meagar Avatar answered Sep 24 '22 22:09

meagar


argc is the number of arguments being passed into your program from the command line and argv is the array of arguments.

You can loop through the arguments knowing the number of them like:

for(int i = 0; i < argc; i++) {     // argv[i] is the argument at index i } 
like image 29
John Boker Avatar answered Sep 24 '22 22:09

John Boker