Page 1 of 1

Passing arguments to your program How to use & impliment int argc char argv functionality Rate Topic: ----- 1 Votes

#1 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Post icon  Posted 13 July 2007 - 10:51 AM

int main(int argc, char *argv[])
** How to use & impliment int argc char argv functionality aka passing arguments to your program **

In order to allow your program to accept arguments, you need to declair 2 items in your main function. Those being integer argc & the charactor argv array. These two items accept the arguments, & allow you to use them inside of your program. This will both be explained in detail, as well examples will be given.

Argc is the integer value of how many arguments have been passed. It is important to remember that the default value will be the command itself. So for example, if 3 arguments are passed, then argv[1] will be the command, giving argc a value of 4 (0 through 4). If your program absolutly must recieve a specific number of arguments, you can use this value to break execution, & then prompt the user on the usage of your program.

Argv is the other value being passed, & it is a charactor array. You can use argv[] to referance each array, or argv[][] to referance each charactor of each array, or a 2d array.

Code example:
#include <stdio.h>

// main program
int main(int argc,char *argv[]) {
	int i=1;
	// decode arguments
	if(argc < 2) {
		printf("You must provide at least one argument\n");
		exit(0);
	}
	// report settings
	for (;i<argc;i++) printf("Argument %d:%s\n",i,argv[i]);
	return(0);
}




Usage examples:
server:/home/user/code>./prog
You must provide at least one argument



server:/home/user/code>./prog help me out
Argument 1:help
Argument 2:me
Argument 3:out



Is This A Good Question/Topic? 0
  • +

Replies To: Passing arguments to your program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Posted 15 July 2007 - 07:21 AM

For clarity's sake, the argv, or argument vector, parameter is actually a pointer to an array of string pointers.

Alternate:

http://publications....ts_to_main.html
Was This Post Helpful? 1
  • +
  • -

#3 tralfas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-January 08

Posted 09 March 2008 - 08:53 PM

wow that was very helpful i always wondered what those were for and now i know. thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Panarchy  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 28-November 07

Posted 10 May 2009 - 05:51 AM

Thanks for the tutorial, I was given the link to this in my most recent topic.

I was also told in my most recent topic that we shouldn't be using printf in C++, that we should instead use cout.

Can this tutorial please be rectified?

Thanks in advance,

Panarchy
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Posted 20 May 2009 - 12:10 AM

View PostPanarchy, on 10 May, 2009 - 06:51 AM, said:

I was also told in my most recent topic that we shouldn't be using printf in C++, that we should instead use cout.

Can this tutorial please be rectified?

As long as you understand the difference, use whatever output method that you prefer. Something as trivial as writing to standard output should not require a re-write of the tutorial, since the purpose of the tutorial is to convey the usage of passing arguments to your program.

As well, there is no C Tutorials to submit this to. So again, I see no reason to change such a minor detail. I hope that you can still find this tutorial useful.
Was This Post Helpful? 1
  • +
  • -

#6 nathan24  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 25-November 11

Posted 25 November 2011 - 09:53 PM

The example code looks great, and nicely demonstrates how to access arguments passed into a C/C++ program.

However, I believe the description is a little misleading:

Quote

argc is the integer value of how many arguments have been passed. It is important to remember that the default value will be the command itself. So for example, if 3 arguments are passed, then argv[1] will be the command, giving argc a value of 4 (0 through 4).


Note that the command itself will be in argv[0], not argv[1]. In the example you mentioned above, argc will be 4 like you said, but argv will only contain 4 strings (so argv will only have valid entries for 0 through 3--not "0 through 4"). That is:

argv[0]: program name
argv[1]: first argument
argv[2]: second argument
argv[3]: third argument
argv[4]: Out of bounds--don't access this in the example that you give

Again, the sample code is correct and you obviously know what you're doing. It's just the description that is (in my opinion) a little misleading.

Sorry to add this to an old thread, but this was the first website that google pointed me to when I searched regarding passing args to a C++ program, so I thought I might add this for whoever else stumbles upon this.

Kind regards,
Nathan
Was This Post Helpful? 2
  • +
  • -

#7 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Posted 25 November 2011 - 10:19 PM

Don't be sorry. Anything to improve the quality of content for others to learn is greatly appreciated.
Was This Post Helpful? 1
  • +
  • -

#8 Xploit_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 11

Posted 08 January 2012 - 12:21 PM

I usually put all the arguments into a vector so they are easier to work with:
#include <string>
#include <vector>

vector<string> BuildParameterList(const int & argc, char **argv);/**
for simplicity, store the main arguments in a vector
argc will then be derivable
pre: the main arguments are passed in
post: a vector of strings is returned. each element is an argument
NOTE: no arguments creates an empty vector
      the program name is not added to the vector */

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

	vector<string> parameterList = BuildParameterList(argc,argv);

	return 0;
}
vector<string> BuildParameterList(const int & argc, char **argv){
	vector<string> returnList;

	//loop through and store the arguments into a vector
	//the loop starts at 1 because the program name is considered the first.
	//the program name is not added to the vector
	for (int i = 1; i < argc; i ++){
		returnList.push_back(argv[i]);
	}

	return returnList;
}

Was This Post Helpful? 0
  • +
  • -

#9 Jonand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-September 12

Posted 26 September 2012 - 07:22 AM

Hello ,

Your it is very usefull .I am a beginner in c++ and i am trying to make an parser for the command line .
I have copy-ed your code and run it. And here is my question : how do i put my data in the console window ? if i
run the program the window will only flash and i am not able to input any data .Don't i need to store that date in
an string or something ?

Waiting for your rely. Thanks .
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Posted 26 September 2012 - 07:25 AM

The answer to your question is common for Windows/console development & can be found in our C++ FAQ.

I'm glad you found the tutorial useful!
Was This Post Helpful? 1
  • +
  • -

#11 Jonand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-September 12

Posted 26 September 2012 - 07:53 AM

Hey ,thanks for the fast replay .

I have manage to keep the console window of your exemple open with cin.get(); .But the argc wont increment no matter how
many arguments i enter in the console . Could you elaborate a little on this mater .I would be really gratefull .

Thanks a lot again .
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Posted 26 September 2012 - 09:34 AM

View PostJonand, on 26 September 2012 - 10:53 AM, said:

the argc wont increment no matter how many arguments i enter in the console

Please open a new topic & provide your code.

"My car makes a 'clank noise', what could be wrong & how do I fix it?!"
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Posted 31 October 2012 - 07:49 AM

View Postnathan24, on 25 November 2011 - 11:53 PM, said:

Note that the command itself will be in argv[0], not argv[1]. In the example you mentioned above, argc will be 4 like you said, but argv will only contain 4 strings (so argv will only have valid entries for 0 through 3--not "0 through 4"). That is:

argv[0]: program name
argv[1]: first argument
argv[2]: second argument
argv[3]: third argument
argv[4]: Out of bounds--don't access this in the example that you give


Actually argv[4] is not out of bounds. The parameter argv[argc] is guaranteed to be a NULL pointer in C and 0 in C++. From the C11 standard:

Quote

If they are declared, the parameters to the main function shall obey the following
constraints:
The value of argc shall be nonnegative.
argv[argc] shall be a null pointer.


Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1