Very basic C++ question - Shebang?

Posted on 2006-03-25
Last Modified: 2012-08-13
I have been slowly learning c++.  For most of it, I have just been using vi, but as I am progressing to more complicated areas I decided to enlist the help of an IDE - xCode.

When I start a new main() file, it automatically inserts this:

int main (int argc, char * const argv[])

All of my programs to date have had nothing in the parens.  I tried to look this up in my 2 c++ books but they never really discuss this.  Could someone please explain what is going on in these parens, and what these arguments (I am assuming that is what they are) are used for?

Thank you for your patience with this newbie...

Question by:bnblazer
    LVL 20

    Assisted Solution

    hi  bnblazer,

    every programm can be executed with arguments, i.e.:

    ls -l
    dir c:\

    both "-l" and "c:\" are arguments, which are passed to the program. the first argument, which is passed to a program is the
    name of the program itsself. so the parameter argc is the number of arguments passed and argv is an
    array of char-arrays(null-terminated string) with the arguments:

    execute this:

    #include <stdio.h>

    int main( int argc, char * const argv[] )
        int intIndex;
        for( intIndex = 0; intIndex < argc; ++intIndex )
            printf( "argument %d is \"%s\"\n", intIndex, argv[ intIndex ] );
        return 0;

    hope it helps :)
    LVL 9

    Accepted Solution


    An integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.


    An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked, argv[1] is the first command-line argument, and so on, until argv[argc], which is always NULL.

    *The first command-line argument is always argv[1] and the last one is argv[argc – 1].

    LVL 9

    Assisted Solution

    a little explanation ..

    char* argv[] = {"name of program", "parameter1", "parameter2"};

    When the program is executed, argv is filled with the above arguments.

    for example:

    myprogram.exe -n 1
    char* argv[] = {"myprogram.exe", "-n", "1"};

    regards :o)

    LVL 1

    Author Comment

    Thanks for the fast and helpful replies.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now