Find the answer to your Linux question:
Results 1 to 2 of 2

Thread: c++ and linux

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    c++ and linux

    I'M learning c++ but I'M doing so from a linux environment. I need to know how to receive command line arguments in C++. I know in bash you use $1, $2, $3, etc... . Thanks.

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Since the ancient days of C, the command line arguments are passed through a couple magical variables available to main():

    $ cat HelloWorld.cpp
    #include <iostream>
    using namespace std;
    int main(int argc, char **argv) {
       for (int i=0; i<argc; i++)
          cout << i << ": " << argv[i] << "\n";
       return 0;
    $ ./HelloWorld 'this is a' test
    0: ./HelloWorld
    1: this is a
    2: test
    I hope this helps. argv[0] is typically the name of your program - which is not as silly as it might sound. Using that, you can often figure out where your program lives or you can do different things depending on how it gets called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts