Hi guys,
I have just started to learn c++ and I am working with robots. Every program is starting with
main(int argc, char *argv[]) definition . I don't understand what does it mean?I am very glad if somebody canexplain for me .

Recommended Answers

All 20 Replies

thanks for your reply, I know the main function but the I couldn't undertand the term (int argc, char *argv[]). When we want to run the program, do we need to define argc and argv. I could.t undertand these parts.

If you just type in
./robot
then you don't need argc or argv

But if you were to type in say
./robot -input=file.txt
then you would need to use argc and argv to read the rest of the command line.

hi ,

#include <stdio.h>
    

main(int argc, char* argv[])
{
     int i;

     printf("argc = %d\n", argc);
   
     for (i = 0; i<argc; i++)
          printf("argv[%d] = %s\n", i, argv[i]);


}

argc is the number of argv. In one sense.
argc is the number of arguments like -b,-ab . In other sense.

argv is the argument . like -b , -ab ..., but first argv[0]=filename with full path.

Can robot be coded in C++, I thought AI languages like Lisp ,etc are more suitable for that? What module in robot are you trying to understand ?

commented: Genius -1

>Can robot be coded in C++
Of course it can.

>I thought AI languages like Lisp ,etc are more suitable for that?
Suitable and capable aren't synonyms.

dear friend
hello

I have the same problem as you have, and I could not find the ans.

It will make me happy to discuses with you about this problem and transmit the information to each other.

regards
amir

>> , and I could not find the ans.

Did you read any of the replies in this thread? :icon_rolleyes:

yes i did
but i think they were not enough.
thanks

to be honest i do think the replies explained it very well, what exactly do you want to know if the current replies are not good enough?

the answers are so general, for example it is unclear for me where should argc and argv initialize if needed. and some other details.
if it is possible send me a code that these were be used in main part of program.

by the way there is not a reason for me not to be honest.

Member Avatar for embooglement

argc and argv are the command line arguments. argc (argument count) is the number of arguments, and argv (argument vector) is an array of strings representing the arguments. These arguments are filled in automatically when the program is run. argv[0] will always be equal to the name of your program, even when run without arguments, and thus argc will always be greater than or equal to 1. So if you ran a program called "Prog.exe" on the command line like this: "Prog.exe -a, -b", you would, in essence, be calling "int argc = 3; char argv[] = { "Prog.exe", "-a", "-b" }; main(argc, argv);" However, that is all done for you, you do not need to worry about initializing argc and argv, nor do you even need to worry about them unless you want command line arguments.

it was useful and teach full exactly for me.
thanks all.

the answers are so general, for example it is unclear for me where should argc and argv initialize if needed. and some other details.
if it is possible send me a code that these were be used in main part of program.

by the way there is not a reason for me not to be honest.

you never really specified what you wanted to know

Look Argc stands for the argument counter Argv is the array of arguments here is the simplest demonstration.

#include <iostream>

int main(int argc,char *argv[]){
int x;
x=argc;
cout<<x;
for(;x!=0;x--){
cout<<argv[x]<<endl;
cin.get();
return 0;
}

It's not hard to understand
For example if I did
./counter arg1 arg2 a v f d r 1
The program should print
1
r
d
v
a
arg2
arg1
counter

Hi guys,
I have just started to learn c++ and I am working with robots. Every program is starting with
main(int argc, char *argv[]) definition . I don't understand what does it mean?I am very glad if somebody canexplain for me .

It appears that the software language that your robot software is using is C. For all C programs, when your robot is powered on, the main(int argc, char *argv[]) function is the entry point or the first place that your robot looks for instructions.

Break down of main(int argc, char *argv[]) is as follows:

If your robot is under the control of a command line interface like 'command.exe' in windows, then you'll need to understand what 'argc' and 'argv[]' mean. Let's say the name of your robot control program is 'robotGo.exe' and is located on your wireless windows laptop. Let's also suppose that your robot is a wireless race car that listens to the robotGo.exe program from the wireless laptop. Now if you had written the robotGo.exe program to take two parameters in like 'go' and 'take_off_mph=20' you would have to execute the command in windows command.exe like this:

c:\robotGo.exe go 20

That would tell the race car to take off at 20 mph. In your code you would have probably implemented your program like this:

int main(int argc, char * argv[])
{
   //argc is the number of arguments that are entered after 'robotGo.exe'
   //separated by spaces. 
   //example: 
   //in windows command.exe you enter c:\robotGo.exe go 20
   //then argc = 2.

   //argv[] is an array that holds the strings or text that was entered 
   //after 'robotGo.exe'. 
   //example: 
   //in windows command.exe you enter c:\robotGo.exe go 20
   //then argv[0]="go" and argv[1]="20" 
   //NOTE: argv[1]="20" is a string and not a number. You will have to 
   //use the ascii to integer function, atoi(), in order to convert
   //the string "20" to a numerical integer.
   
   //------------------------------------------------------------------
   //example implementation
   //------------------------------------------------------------------

   int go_command_detected=0;
   char * argv[2];

   //using strcmp() to see if the contents of argv[0] match "go"
   go_command_detected = strcmp(argv[0],"go");

   if(go_command_detected)
   {
      int speed;

      //converting the string speed to an actual number
      speed = atoi(argv[1]);

      setRaceCarSpeedTo(speed);

      //this function tells the race car to go with the new speed.
      makeRaceCarGo();
   }

}

If you need a better explanation, let me know.

If you need a better explanation, let me know.

Since he hasn't been here since Jan 4th, I wouldn't hold my breath waiting for him.

awesome post bro jjwitherspoon.. Thanx for the brief explanation!

Hi,

I know this thread is a computer science topic, and computer science happens to be my main interest; however, I feel I have something useful to contribute of a different nature. Take it for what you feel it's worth. It's just some observations I made while reading through this thread.

"enough" and "good enough" are not synonymous. Good implies a distinction of quality while enough implies quantity.

I'm not certain where the person lives because that information is not recorded in this thread. However, ako6626 signs his name "amir". Amir's first post in this thread begins with a salutation (seems to be directed toward the original poster) - "dear friend hello"

This is very different than what someone from US culture may be used to. While I personally like what that language communicates to a person, it occurs to me that not everyone typically speaks to one another this way. Perhaps differences in heritage play a part in the reason.

Why do I mention this? Because differences in language and communication style can sometimes lead to misunderstandings...

Later in the thread a person uses a phrase in their post: "to be honest" to which it is replied: "by the way there is not a reason for me not to be honest." It seems there is some misunderstanding here.

In US culture, what it usually meant by "to be honest" is not directed toward the other person in the conversation. It's an idiom that's meant to convey that the person believes what they are about to say is the truth.

As I mentioned before, these are just some observations I thought may be useful in some way. For the record, I came upon this thread because I, myself, was wondering what the answer to this question is. You see, I'm a computer science major at a University in the US; and, so far, we have only ever used "int main(void) OR int main()" --same thing.

By the way, I also thought it was an excellent post and thread. I learned a lot. Thank you.

Peace

I concur with ClientAlive. I read through the discussion myself and actually did some google searching for the answer to the above question and I found no answer that really answered the question until I stumbled upon jjwitherspoon's response.

quite a few of the answers mentioned before jjwitherspoon's were redundant in response compared with other google searches and were more or less copy pasted from someone else's attempt to answer the question before.

jjwitherspoon gave a very personal response to the question that he understood for himself probably because he has used the information before and has applied it.

try and follow jjwitherspoon's approach to a response so that people can learn from you rather than feel hopelessly lost at the Regurgitations of other people's answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.