• 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
https://www.neowin.net/forum/topic/190198-c-int-mainint-argc-char-argv/
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

  • 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... :(

  • 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

  • 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...

  • 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.
  • 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

  • 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

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

    • No registered users viewing this page.
  • Posts

    • Looks like the 7.1 is fake effects, can it at least do real 5.1? It says 'virtual 7.1' in all descriptions.
    • You can't, if you didn't notice, it doesn't support surround at all, it's right in the spec list.
    • Hi — I’m always interested in soundcards. Like displays, I just want to know I’m getting as much clean “sonic juice” into my brain as possible as the years take their toll. I’m not entirely sure what to take away from this review, though. It doesn’t really tell me whether the AE‑X is a good product or who it’s actually for. Most of what I’m getting is: there’s a driver to install, here’s what it looks like, and here’s what’s in the box. There’s a lot of emphasis on the SPDIF input. When you mention not needing to switch headphones between console and PC — does that mean the PC has to be powered on just to pass audio through? That seems like a fairly big waste of energy. Is this more something a streamer would use alongside a capture card? How are you testing the sound? (Also, you might want to clarify that you’re using the headphones in wired mode when you mention they’re wireless.) You mention the lack of EMI shielding — how much real‑world difference does that make compared with typical motherboard audio? On multi‑channel: what exactly isn’t supported? Does this mean Windows spatial audio (Dolby Atmos for Headphones, DTS Headphone:X, etc.) won’t work, or just that the card itself doesn’t decode surround formats? And are there any true multi‑driver “surround” headphones left that would even use that? You also highlight support for high‑impedance headphones — but what does that translate to in practice? How does it compare to driving the same headphones from a normal device, and does it make any difference for everyday, lower‑impedance models? In short, who is this card actually targeted at?
    • Yes, THIS is wordart, not the styling that can now be done. Wordart was all about those curvy words, that you could change the path of, like making words go around a circle. I don't think it can be done now, right?
    • Just saw a news report of a Waymo driving into a flooded road.
  • Recent Achievements

    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
    • First Post
      DrWankel earned a badge
      First Post
    • Reacting Well
      DrWankel earned a badge
      Reacting Well
    • Week One Done
      Supreme Spray LV earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      170
    3. 3
      PsYcHoKiLLa
      88
    4. 4
      Steven P.
      76
    5. 5
      Michael Scrip
      74
  • Tell a friend

    Love Neowin? Tell a friend!