We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Very basic C++ question - Shebang?

bnblazer
bnblazer asked
on
Medium Priority
383 Views
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...

Brian
Comment
Watch Question

CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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;
Commented:
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)

Author

Commented:
Thanks for the fast and helpful replies.

Brian
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.