• 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

    • I hope this encodes in to AV1 or AV2 as currently tiktok uses h265 and h264.
    • Qualcomm reportedly in talks to build custom video chips for TikTok parent ByteDance by Karthik Mudaliar Qualcomm is reportedly in advanced discussions to provide custom chip-design services to Chinese tech giant ByteDance, the same company behind TikTok. According to a report from Reuters, Qualcomm could be involved in designing custom silicon tailored for ByteDance's massive data-center workloads. If it goes through, the deal would make ByteDance one of Qualcomm's early anchor customers for its fastly growing custom chip-design division, For years, Qualcomm was the king of making smartphone processors and modems. The company has also been moving into the PC ecosystem and other formats such as on-device AI for Android XR headsets. However, this particular deal is about Qualcomm's custom Application-Specific Integrated Circuits (ASICs). For a platform like TikTok, ByteDance needs hardware that can help it ingest, process, and serve billions of short-form videos daily. Generalised hardware is no longer the most cost-effective and efficient route, which is why ByteDance is trying to develop custom Video Processing Units (VPUs). VPUs designed specifically for ByteDance’s algorithmic needs could drastically reduce data-center power consumption and improve encoding speeds at an unprecedented scale. The underlying tech behind these processors is actually from Qualcomm's recent acquisition of AlphaWave Semi, a high-speed connectivity specialist company. By combining AlphaWave’s high-bandwidth IP with Qualcomm’s architectural expertise, the company could begin mass production by the end of 2026, if the talks go through. All this also comes at a time when U.S.-China tech relations have dwindled. Escalating trade frictions between Washington and Beijing have severely impacted the export of high-end AI chips from U.S. firms like Nvidia, AMD, and Lam Research. Yet, the Qualcomm-ByteDance discussions show that U.S. tech companies are still actively seeking growth avenues and are open to doing business with China, where regulators still permit. Reuters notes that the outcome of this deal could be uncertain, and ByteDance might also seek partners other than Qualcomm. via Reuters | Image via DepositPhotos.com
    • Look who's back!
    • I wonder how driving laws around the world will change. No way to really tell if people are using phone. Same with smart watches i guess even now and those silly built in tablets for controlling the car instead of buttons.
  • Recent Achievements

    • Rookie
      DaviKar went up a rank
      Rookie
    • Dedicated
      HidekoYamamoto94 earned a badge
      Dedicated
    • One Month Later
      timbobit earned a badge
      One Month Later
    • One Month Later
      nates earned a badge
      One Month Later
    • Week One Done
      Almohandis earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      454
    2. 2
      +Edouard
      161
    3. 3
      PsYcHoKiLLa
      111
    4. 4
      Michael Scrip
      83
    5. 5
      Steven P.
      69
  • Tell a friend

    Love Neowin? Tell a friend!