Click here to Skip to main content
15,123,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've seen in many programs that they use
conditions, why and when they use this conditions?

Also I've seen in many programs they write argv[1]like
or other places in a program.

When I write
in my programs, I get no result! I dont know why? and aalso when I write argv[1] in my programs I get logical errors!

But when I write
I get a path like

Can you explain when and how can I use argc and argv arguments?

Thank you

I suppose every book about the C programming language explains that. Anyway I would Google for[^].
hor_313 20-Jun-14 7:04am
I've read many c++ books and Googled it alot. when I ask it here it means I couldn't find my answer in other places!
[no name] 20-Jun-14 7:45am
You will have a very hard time convincing anyone that you could not find this information yourself.
Stefan_Lang 20-Jun-14 9:05am
Just googling for the keywords 'argv' and 'argc' gives you plenty of useful links on the very first result page. What keywords did you use if not these?
argc and argv are the two parts of the command line string which are passed to your application if it is started from the command line, or given optiona arguemnets when started as a process from within a different application.

For example, if your app is registered as handling all files with an extension ".MyAppFiles" and the user double clicks on "MyFile.MyAppFiles" in Windows Explorer, you app will be started and given the file name:
as a command line argument - so argc will be the count, and argv will be an array of strings (or more accurately a char**) which contains the actual data.

All the code you are looking at is doing is working out if it has enough parameters to continue and do its job in safety.
hor_313 20-Jun-14 7:18am
Ive seen in many example codes they use argv[1] , but when i copy that code and try to run,it doesn't work in my computer! why?
Stefan_Lang 20-Jun-14 9:10am
If you don't specify a command line argument then argv won't have a second entry. That's why you should check if argc is at least two, before reading argv[1]!

However, why don't you just follow the first solutions advice to go google for a more detailled explanation? We won't be reciprocating every last bit of info there is to know about C/C++ over the next years just because you can't be bothered to search for widely available information!
hor_313 20-Jun-14 7:19am
Also when I write "cout<<argv[1];" in my programs I have no results!
CHill60 20-Jun-14 8:02am
No results means that you are not passing any parameters into your program
hor_313 20-Jun-14 8:07am
You mean all example codes that use of argv[1] must be execute from command line?! and they wont work if I try to run them in my IDE?
argc is the count of the number of parameters in argv and will be any number from 1 upwards. The first parameter argv[0] is always the path to the executable program, e.g.

The remaining pointers are the parameters passed on the command line, or by Explorer when the user starts the program by double clicking a file name. So if you call the program from the above path like:
program_name foo bar "A long string of nonsense"

argc will equal 4, and argv values will be:
argv[0] : /home/user/program_folder/program_name/bin/program_name/Debug/program_name
argv[1] : foo
argv[2] : bar
argv[3] : A long string of nonsense
hor_313 20-Jun-14 8:10am
I know what you said, But my question is why in many example codes they use of argv[1]. Is that mean we must run program only from command line to work?
Richard MacCutchan 20-Jun-14 8:58am
No, if your program has a file association set in Windows Explorer, then when you double click a file of that type your program will be launched with argv[1] set to the path of that file. Similarly if your program allows drag and drop you would get the same thing. The main point is, that you must always check the value of argc first, to see how many parameters you have received.
hor_313 20-Jun-14 9:37am
this only applies to Windows or true in Linux?
hor_313 20-Jun-14 8:11am
Can you explain this part of your sentence clearly(may with example): "or by Explorer when the user starts the program by double clicking a file name."
Richard MacCutchan 20-Jun-14 9:00am
You can test this yourself quite easily by writing a short test program, that just lists all the parameters sent to it at start up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900