Very basic C++ question - Shebang?

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

Brian
LVL 1
bnblazerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ikeworkCommented:
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 :)
ike
0
DrAskeCommented:
argc

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

argv

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

regards;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrAskeCommented:
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)

0
bnblazerAuthor Commented:
Thanks for the fast and helpful replies.

Brian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.