What is the purpose of these arguements in main: "argc" and "argv"?

I have seen certain codes with these arguments inside the parameters of the main function.

 
int main(int argc, char *argv)


From what I understand, argc is usually a numerical type while argv is usually a pointer. And by assigning a value to argc in the body of main, you're pretty much passing the value as an argument(but not sure what for). And the same concept applies to argv(I think).


But that's about all I get haha. So can someone enlighten me about these functions in simple detail?















Last edited on
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


PS: Copied straight from stack overflow
vxk wrote:
PS: Copied straight from stack overflow

So why didn't you give a link then?
http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean
hmm, I still don't think I completely understand the concept here. So argc contains the amount of arguments that are accumulated by argv. But how can I accumulate the arguments? I have tried the program in the example you copied but it only shows the name of the file and it's directory.

So the file itself is an argument right? if that's the case how can I add more arguments?

p.s. I want a detailed explanation and not just copy and paste whatever result you get from google. I have already searched through google but I'm still having some trouble grasping the concept.
Last edited on
Have you any experience with the command line interface? (Usually called command prompt, terminal or console).

On Windows you can press Windows flag key+r, type "cmd" and press enter to bring up the Command Prompt. You can also find it in the menus somewhere.

To run a program you write the name of the program. If you write other words after the program name they will be passed to the program as arguments. The program can read these arguments by using argc and argv.

One example of a program that is already available is called mkdir. It is used to create a directory (folder). If you type "mkdir mypictures" and press enter it will create a directory named "mypictures". I hope this demonstrates the usefulness of being able to pass arguments to programs.
Last edited on
Yes I have some experience with "cmd".

So the program name is basically the name of the file right? For example I have made a file named "argc and argv main arguments.cpp" in codeblocks, do I just type the entire name of the file in cmd to access it?
By program name I meant the name of the executable file.

If the current directory is the same directory as where the executable file is located you just have to type the filename, otherwise you will have to type the whole file path (relative or absolute). If you want to change the current directory you can use the cd command.
Last edited on
The arguments are a way to get user input without having to prompt them for all of it. Here is a snip-it from a small C program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// ...
enum {ARG_NAME,ARG_OUTFILE,ARG_DUR,
       ARG_HZ, ARG_SR, ARG_AMP, ARG_NARGS} ;

int main(int argc, char** argv)
{
	int i, sr, nsamps;
	double samp, dur, freq, srate, amp, maxsamp;
	double start, end, fac, angleincr;
	double twopi = 2.0 * M_PI;
	FILE* fp = NULL;

	if(argc != ARG_NARGS) // if the number of arguments is not right
	{
                // print a list of the arguments
		printf("Usage: tfork2 outfile.txt dur freq srate amp\n");
               // alternate form:
               // printf("Usage: %s outfile.txt dur freq srate amp\n", argv[ARG_NAME);  
  		return 1;
	}

	fp = fopen(argv[ARG_OUTFIlE], "w" );
	if(fp == NULL)
	{
		printf("Error creating output file %s\n",
			   argv[ARG_OUTFILE]);
		return 1;
	}

        // assign argument values to variables 
	dur = atof(argv[ARG_DUR]);
	freq = atof(argv[ARG_Hz]);
	srate = atof(argv[ARG_SR]);
	amp - atof(argv[ARG_AMP]);
	//...
}

The enum is a good way to remember which argument goes to which variable.

<edit>
If you tried to run this in C:B by clicking run it would print the error message:
Usage: tfork2 outfile.txt dur freq srate amp

You have to use cmd like Peter87 described. In this case you would type something like:
tfork2 output.txt 6 22.2 44.1 7
Last edited on
Topic archived. No new replies allowed.