• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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
0
bnblazer
Asked:
bnblazer
  • 2
3 Solutions
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now