How to use command line arguments? what are they?

Pages: 12
What actually is it and how do you use it, with an example?
1
2
3
4
5
6
7
8
9
#include <iostream>

int main(int argc, char ** argv)
{
	if (argc>0) {
		for (int i=1; i<argc; i++)
			std::cout << argv[i] << std::endl;
	}
}


Program prints arguments if given. Variable argc holds amount of arguments given and argv holds them.
argv[0] is program name (for example a.out)
My question really is


How do you "GIVE" a program arguments? Ive read completely on CLA and know how to code it but how to pass to a programme the arguments?
Yes, very serious.

Look at this simple code:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
/* echo command-line arguments; 1st version */
main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
printf("%s%s", argv[i], (i < argc-1) ? " " : "");
printf("\n");
return 0;
}


Its saying argument 1 and upwards are printed out with a space after them and the last argument has no space after it.

Unless im blind but all the links provided do not tell me how to set argument 1 as X or argument 2 as Y. For the code above, how do I determine what argument 1 or 2 is in the first place?
Well, passing arguments to a program is part of the basics of using a computer, so you're expected to know that long before you even think about learning programming.
Just read the wikipedia article.
Unless im blind but all the links provided do not tell me how to set argument 1 as X or argument 2 as Y. For the code above, how do I determine what argument 1 or 2 is in the first place?


1) The arguments for main() are given by the Operating System.
2) Which means that you actually give the arguments to the executable itself.
3) You do this by starting a Console and running the program like this:
prog.exe arg1 arg2 arg3
4) The first argument argv[0] is the program path and is autoset when you start your program.
5) You can have as many arguments as you want, the OS will take care to give main() the list of arguments **argv and how many arguments were given argc.
6) The arguments will be stored in **argv by the order they were given.
7) You can usually use whatever names you want instead of argc and argv as long as you respect their meaning. This is not recommended as to not confuse others or maybe the compiler.
8) The arguments are separated by spaces when you give them. If you want to give an argument which contains a space, you might use:
prog.exe arg1 "arg2 with space" arg3

Last edited on
Athar wrote:
Well, passing arguments to a program is part of the basics of using a computer, so you're expected to know that long before you even think about learning programming.
Just read the wikipedia article.


I assure you I have read it as did I a few articles on it.
Yet I do not know, in that simple simple piece of code posted above, how to make argv[1] equal to something and, say, argv[2] equal something else. How do I "pass" to it or "give" it these "arguments"?

Look, heres another explanation on the internet:
In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.


That actually explained nothing. Ive also programmed for a year now, ive used a computer for over 10 years and I dont know how to pass it, so I dont I was personally never expected to know it.
Last edited on
That actually explained nothing.

Actually, it did.
Here's another article: http://en.wikipedia.org/wiki/Cmd.exe

Ive also programmed for a year now, ive used a computer for over 10 years and I dont know how to pass it, so I dont I was personally never expected to know it.

What did you do with the computer all these 10 years to never even have heard of the command line?
Last edited on
closed account (z05DSL3A)
Sputnik,

What OS are you using? What IDE?
i think its perfectly possible to use a computer for 10 years and never use the command line. for the average user that comes up maybe um 0 times. unless your using some ancient zip program that has no interface.

anyway i think catfish pretty much cleared it up for him.
Catfish wrote:


1) The arguments for main() are given by the Operating System.
2) Which means that you actually give the arguments to the executable itself.
3) You do this by starting a Console and running the program like this:
prog.exe arg1 arg2 arg3
4) The first argument argv[0] is the program path and is autoset when you start your program.
5) You can have as many arguments as you want, the OS will take care to give main() the list of arguments **argv and how many arguments were given argc.
6) The arguments will be stored in **argv by the order they were given.
7) You can usually use whatever names you want instead of argc and argv as long as you respect their meaning. This is not recommended as to not confuse others or maybe the compiler.
8) The arguments are separated by spaces when you give them. If you want to give an argument which contains a space, you might use:
prog.exe arg1 "arg2 with space" arg3



Thats Exactly what I needed!

Thanks catfish!
I opened up command prompt and typed in the directry of the programme followed by the arguments seperated by spaces and it gave an output just like the code said it would.

Now that my question is solved, one more Q:
Is this not a "dead" feature? I mean, this is useless nowadays surely? Because who runs programms from command prompt or DOS?
Is there a more "modern" way to use it? Like in windows XP for example?

Thanks again
Last edited on
It's not a dead feature because not all Operating Systems are GUI-only, especially those for servers. And believe it or not sometimes its faster working in a shell than clicking away with the mouse.
there is quit a few utility programs that run the command prompt. i know there is one that sorts mp3s and divides them into into their own folders. i dont remember where i saw it but its out there.
Sputnik wrote:
Is this not a "dead" feature? I mean, this is useless nowadays surely? Because who runs programms from command prompt or DOS?
If your writing a script to do something, you can use command line parameters to invoke an application with specific predefined instructions.
closed account (z05DSL3A)
Is this not a "dead" feature? I mean, this is useless nowadays surely? Because who runs programms from command prompt or DOS?
System admins.

Is there a more "modern" way to use it? Like in windows XP for example?

I use command line arguments to pass in configuration info/reset things etc. You can type the command in or create a short cut with the the arguments there.
All the net utilities are command line programs (ping, netstat, ipconfig, tracert) and even the average user should come into contact with one of them sooner or later. Lots of compression programs are command line programs (UPX first and foremost). So are all compilers. And of course tons of utilities for various different things found on the internet.
And then there are batch files/shell scripts which allow to automate repeatedly performed actions by executing other programs in a specific order, all without having to know a single thing about "real" programming.

Even outside of the command line, one should have learned about program arguments during troubleshooting when being told to start an application with specific arguments by customer support or technically versed friends, usually by editing the target of a shortcut.

But eh, I suppose it's possible.
Last edited on
i learned the command from back in the day when you would boot into a dos utility program to create edit and delete drive partitions . of course now its all gui. wasnt like that always though. learning the command line had nothing to do with windows programs for me anyway.
Sputnik wrote:
Is this not a "dead" feature? I mean, this is useless nowadays surely? Because who runs programms from command prompt or DOS?


Not all commandline input is input by the user. Some is input by the OS when the program is run.

Say, for example, you want to open X file with Y program. Windows has Y program associated with X's file extension. So when you double-click on X's icon, Windows dumps "Y <path to file X>" in the commandline, with the expectation that Y will load that file immediately upon program startup.
Last edited on
Pages: 12