• 0

[C++] int main(int argc, char* argv[])...


Question

Hey,

I've only done relatively simple console-based C++ programming and I've never used any parameters in the main() function. However, I see it quite frequently or every so often in sample or other people's code.

int main(int argc, char* argv[]) { ... }

What the hell is it for? :rolleyes: And why don't I see people actually using argc or the array argv[] in their program!

Thanks :yes: Hmm, there is probably no good reason I want to know this other than curiosity, heh... :rofl:

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

The argc and argv values are passed into the entry point when an application is executed by the runtime (mainCRTStartup.)

argc stands for Argument Count. This variable contains the number of arguments passed into the application.

argv stands for Argument Values. This is an array of strings which contains the values passed into the application. argv[0] is always the name of the application.

What they basically do is allow you to use the command line parameters passed to your application. Even if you declare them in main(), you are not forced to use them, however some compilers might give you a warning for "unused variable."

Hope that helps,

bwx

Link to comment
Share on other sites

  • 0

Hey thanks for that answer man, got it. So in C++, you can declare it if you want, and you can use it if you want. I've done some basic Java, you don't seem to have the option there do you? If you leave it out the compiler seems to grumble... :(

Link to comment
Share on other sites

  • 0

Dear fault,

you should declare argc and argv in any case.

As it's just clean coding. Java simply encourages clean coding.

Why you have not seen it is very likely because you have not looked very much at commandline tools.

imageing this pseudo applicaion:

$ switch -a fileA -b fileB

wich would simply copy fileA to fileB and fileB to fileA

now the code would look like the folloing ( PSEUDO CODE )

int
main (int argc, char * argv[])
{
  int i=0;
  char * a[20];
  char * b[20];
  if (argc < 5) // 1: switch, 2: -a, 3: fileA, 4: -b, 5: fileB
    return EXIT_FAILURE;
  for(i=1;i<argc;i++) // we can skip the first - switch is not importatnt to us.
  {
    if (argv[i][0] == '-' )
      switch (argv[i][1])
      {
         case 'a': a = argv[i+1];
         case 'b': b = argv[i+1];
      }
  }
}

I know there are lots of flaws in there. But I think your get the idea.

this would enable you to even run:

$ swicht -b fileB -a fileA

that's the use of argv,argc in real world up to my knowledge.

kindest regards,

Moritz "neofeed" Angermann

Link to comment
Share on other sites

  • 0
you should declare argc and argv in any case. As it's just clean coding. Java simply encourages clean coding.

If you have need for command line parsing, by all means use them; however, it does use resources and if you have no intentions of using it... don't declare them...

Link to comment
Share on other sites

  • 0

Cheers neofeed. I get where you're coming from. Didn't think of that...

Damn, you're a busy little dude. You should totally go into computer engineering then :D
Who me or neofeed? :huh:
If you have need for command line parsing, by all means use them; however, it does use resources and if you have no intentions of using it... don't declare them...
Good point, cheers for the advice.
Link to comment
Share on other sites

  • 0
If you have need for command line parsing, by all means use them; however, it does use resources and if you have no intentions of using it... don't declare them...

yea right!, because they are in mem anyway, huh?

if you are such a resource nazi, you could do something like:

for(int i=0;i<argc;i++) free(argv[i]);
free(argc);
free(i);

at least now you don`t carry those `possible` arguments no longer in your holy resource reservoar.

kindest regards,

Moritz "neofeed" Angermann

Link to comment
Share on other sites

  • 0
Damn, you're a busy little dude. You should totally go into computer engineering then

Who me or neofeed? 

You! You're all inquisitive and such.

ps: Computer engineers kick ass!!!

Link to comment
Share on other sites

  • 0
yea right!, because they are in mem anyway, huh?

if you are such a resource nazi, you could do something like:

for(int i=0;i<argc;i++) free(argv[i]);
free(argc);
free(i);

at least now you don`t carry those `possible` arguments no longer in your holy resource reservoar.

kindest regards,

Moritz "neofeed" Angermann

The point is that certain things do require minimal use of resources...

Second, a good rule of thumb to 'clean' programming is to use the least amount of resources as possible - declaring the command parsing variables does use resources, and it's a complete waste if you don't intend on using it...

Not to mention, it could add a fault or failure point within your code that isn't necessary...

True, it's not a major waste of resources, but it does make a difference, and adding something that isn't necessary or used is 'bad' programming

Link to comment
Share on other sites

  • 0

If you remove certain pices of code you risk losing flexibility/reusability. Who says down the road you won't use command-line params in your app? In this case, it is easy to add in again; in general it may not be.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.