• 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

    • We check out the SKG PS700 Neck Massager by Steven Parker I was offered the chance to test out the SKG PS700 Neck Massager, and full disclosure, they let me keep it regardless of my findings. Anyway, I jumped at the chance due to my long hours sitting at my desk; I figured it could offer some neck pain relief. What's in the box: SKG PS700-2 Neck Massager Rechargeable Battery (inside massager) Type-C USB cable User Manual Quick Start guide 1-Year Warranty In short, everything you need to get started. According to the official listing, here are the key features: Biomimetic Kneading & High Torque Motor: Designed with innovative biomimetic kneading heads that perfectly simulate the touch of human hands. Powered by a high-torque motor, this massager delivers powerful and precise deep tissue relief to effectively target stiff neck muscles and release built-up tension Soothing Heat & Integrated Sound Relaxation: Experience the ultimate Relaxationation with our dual-action approach. The soothing heat function gently warms your neck, while the built-in sound Relaxation provides calming audio tracks, helping you achieve a state of mindfulness and mental tranquility during your physical massage Cordless Convenience & Travel-Ready & Father's Day Gifts: Crafted for maximum portability and ease of use. Its lightweight, cordless design allows you to enjoy a premium massage anywhere without the hassle of tangled wires-whether you're taking a quick break at your desk or winding down at home Versatile Relief for Home & Office: An essential wellness companion for office workers, gamers, frequent travelers, or anyone looking to integrate mindfulness into their daily routine. It seamlessly fits into your lifestyle, providing instant neck relief whenever and wherever you need it Safe & Premium Materials: Manufactured with high-quality, skin-friendly materials to ensure a safe and comfortable experience without irritation. SKG backs this device with dedicated customer service, making it a thoughtful tech-health gift for family and friends App & Bluetooth Music Control: Connect via Bluetooth to control your massage settings through the dedicated app and enjoy your favorite music during your massage session for a fully customizable and immersive relaxation experience Red Light Warmth Technology: Features advanced red light warmth technology that penetrates deep into neck muscles to enhance blood circulation and provide soothing comfort while relieving muscle tension and stiffness Design With all that out of the way, here are my own findings. SKG does not say what materials are used to make the neck massager. However, on the product website, it mentions "soft-touch silicone" with what looks like PU leather cushioning, with the rest being mostly made up of plastics. On the inside of the massager, there are two "biomimetic kneading heads" that are motorized for the different styles of massage, which are not actually listed at all in the paper user manual, but the standard included modes are: De-stress mode, Mediation mode, Relax mode, Shiatsu mode. The massager looks quite premium and is actually very comfortable to wear. This massager is small and light enough to go anywhere, as it doesn't get in the way of anything, so I was able to use it in the chair while writing this review. Unlike the back massager, SKG does not warn in the user guide not to use it for more than 30 minutes a day (or two 15-minute sessions). However, there is a long laundry list of important safeguards to consider before and during the use of the device, and it is warned that the neck massager is not waterproof. It also includes a 1,400mAh battery with a rated power of 14W and input of 5V, which is the standard for up to USB 3.0 power (although the Amperage is not mentioned at all). SKG does not say how long it takes to charge, but a quick calculation at 2A (if that is what it is) would mean it would take roughly 1.5 hrs to charge from empty. In any case, the light around the button changes from orange to green on a full charge. In addition, it is not possible to use the device while it is charging. On the right of the neck massager is the On/Off and modes button, which also acts as a joystick. You can operate all the modes directly from the power button, as well as the app, which I'll get into a bit later: Push up: Short press to adjust Heat levels On/Off button: long press Mode Switching: Short press (while in operation) ➕ Push left: increase Music volume ➖ push right: decrease Music volume Push down: Short-press to turn Music on or off The massager defaults to De-stress mode, and it is not stated anywhere if the neck massager has overheat protection. This time around, regarding heat, the only detail I could find is that it has "triple action soothing heat." The temperature stages are not listed anywhere in the paper manual, Amazon listing, or official website. The heat levels can be adjusted through the app or directly on the device using the joystick button. Usage There's also the SKG Health app, which makes using the massager far easier than feeling around for the button on the side of your neck. If the app is stopped, you are required to log in with a verification code over email, which I am not too pleased with, as this means it will only work that way for however long SKG decides to support it through said app. However, I was not able to get the app to connect to the OS500, which I have reported back to my contact. Bluetooth appeared to be working on the neck massager as it became available to pair with my phone, but the SKG app failed to discover it. Before I forget, there's also a switch next to the USB charging port to deactivate and activate the Voice Prompt, which, when enabled, audibly tells the user when switching intensities, modes, or connecting to the app and informs when the massages start and are completed. That said, on to my likes and dislikes, which are listed below. What I didn't like Unable to connect the Neck Massager to the app Use through the mobile app relies on continued support from SKG What I liked Can be used without the app Cordless use Light and comfortable to wear Heat is also quite comfortable Where to buy: According to the official website, this has an MSRP of $249.99, but is currently $50 (on Amazon). To sweeten the deal a bit more, there's also an in-page coupon that knocks a further $20 off the price. SKG PS700-2 Neck Massager for $179.99 on Amazon (was $199.99) Apply the in-page $20 off coupon for the final price of $179.99 Just like the back massager, this gets a confused thumbs up (due to the cost). However, I cannot rate it through app usage as it failed to connect. As an Amazon Associate, we earn from qualifying purchases.
    • This Samsung T7 external SSD deal lasts less than a day by Sayan Sen Recently we had covered some nice deals of internal NVMe SSDs which include the 4TB TeamGroup G50 for only $400, the WD_BLACK SN7100 2TB for just $243, as well as the Samsung 990 PRO 1TB for $370. If however you require an external SSD for portability and quick data transfers and have a budget of less than $200 the Samsung T7 1TB model is currently on a limited time deal at just $190, it's lowest price in nearly three months. The deal ends today so you better hurry if you need one (purchase link below). The T7 weighs in at just 72 grams meaning it should be fairly easy to carry around helping in the portability department. Via its USB 3.2 Gen 2 interface the T7 promises sequential read speeds of up to 1050 MB/s and writes of 1000 MB/s. It is also fairly robust with a drop protection of up to 2 meters, though bear in mind that this is not waterproof. For that you will have to choose the rugged T7 Shield. The technical specifications of the Samsung T7 1TB are given in the table below: Specification Value Model Code (1TB) MU-PC1T0T / MU-PC1T0H Interface USB 3.2 Gen 2 (10 Gbps) Dimensions (W × H × D) 85 × 57 × 8 mm Weight 72 g Sequential Read Speed Up to 1,050 MB/s Sequential Write Speed Up to 1,000 MB/s Drop Resistance Up to 2 m (6.6 ft) Encryption AES 256-bit hardware encryption Operating Temperature 0°C to 60°C Non-Operating Temperature -40°C to 85°C Humidity 5% to 95% (non-condensing) Shock Resistance 1,500 G, duration 0.5 ms, 3-axis (non-operating) Vibration Resistance 20–2,000 Hz, 20 G (non-operating) Get it at the link below: Samsung T7 Portable SSD, 1TB External Solid State Drive, MU-PC1T0T/AM, Gray: $189.98 (Sold and Shipped by Amazon US) Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • I just wish they would put more love into Virtual Desktops. There is just so much more they could do.
    • Sounds like you just skimmed over the review, I answer all those questions in it. Although I admit I did not test if DTS works over the headphones, when I get some time I will test this. My personal use case is more speaker-driven (I cover this in the review too).
  • 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
      505
    2. 2
      +Edouard
      183
    3. 3
      PsYcHoKiLLa
      84
    4. 4
      Michael Scrip
      78
    5. 5
      Steven P.
      75
  • Tell a friend

    Love Neowin? Tell a friend!