Jump to content
  • Advertisement
Sign in to follow this  
Meta Adam

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

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
int main() does the same thing when you don't need the command line arguments.
When using the longer form, argc contains the number of arguments and argv contains all arguments. Note that the first argument is the path of the application.

Share this post


Link to post
Share on other sites
I'm guessing you're using Dev-C++. That is where I first found the long form of int main().

Like the neoztar said, you can store command line arguements. But, in Dev-C++, this is required to use. Go ahead and try running a simple program without the extra parameters, and I'll bet it won't work (at least, it doesn't work on my computer without them).

Until you start getting into more advanced programming, don't worry about those parameters. Just accept them as required if you have to until you get to that point.

Share this post


Link to post
Share on other sites
Here ya go.

Quote:
Original post by FinalMinuet
Until you start getting into more advanced programming, don't worry about those parameters. Just accept them as required if you have to until you get to that point.
Try not to encourage Voodoo Programming practices [smile].

Share this post


Link to post
Share on other sites
That's what I thought. I started out on our school's version of Visual C++ and I obtained a command-line version of Borland shortly after, so I too had never seen the int main(int argc, char *argv[]) before.

You can probably find a tutorial somewhere on either GameDev or another site that can teach you how to use it.

Share this post


Link to post
Share on other sites
It isn't hard to use. Following Rob's link explains what it does, although in a little different syntax.

Share this post


Link to post
Share on other sites
my_app.exe

int main(int argc, char* argv[])
{
return 0;
}





C:> my_app.exe here are some command line arguments!
| | | | | | |
1 2 3 4 5 6 7


argc = 7

argv has the following information stored:
argv[0] = "my_app.exe"
argv[1] = "here"
argv[2] = "are"
argv[3] = "some"
etc...

Note: I use dev c++, and it doesn't require me to use this syntax. Although, I usually do anyways out of habit.

Share this post


Link to post
Share on other sites
Just what prototypes (forms) are OK? Let's ask Google.

Google says "Hey, I know some dudes at Informit who seem to know what they're talking about.

And those dudes say:

Quote:

Our first example of a function is main(), where C++ programs begin execution. The formats are

int main() { statements; }
int main(int argc, char *argv[]) { statements; }

The first format works with programs that you execute with only a program name. The second format handles programs with character string command-line arguments. The second format makes each argument and the total number of command-line words available to your program. The variable argc is an integer equal to the number of words on the command line, including the program name (argc is always at least one). The variable argv is a pointer to an array of pointers to characters. Each pointer in the array points to the words on the command line. The first pointer, which is argv[0], points to the program name.

NOTE

The following format for main() is safer with programs that have command-line arguments.

int main(int argc, const char *argv[]) { statements; }

With const, argv now points to an array of pointers to constant characters. This arrangement prevents accidental modifications to command-line arguments inside main().

Suppose, for example, we execute a C++ program called com, as follows.

$ com -r file

The first command-line word is com, the name of the program (pointed to by argv[0]). The second word is -r (presumably a program option). The third command-line word is a file name called file.

...

Each element of the pointer array is a pointer to a NULL terminated (\0) character string from the command line. The last element of this pointer array is 0.

We use argv to access both characters and words from the command line

...

NOTE

Always use argv[0] and not your program name (for example, com) in usage messages with cerr statements. This ensures that error messages display the correct program name, even if you rename your file.

The notation argv is a pointer to the i'th word (starting at 0) on the command line. Likewise, argv[j] is the j'th character of the i'th command-line word (both indices start at 0). We return 0 (normal status) at the end of the program.


There's a lot of irrelevant stuff among the next few hits, but this article is also enlightening.

Share this post


Link to post
Share on other sites
Not to scare anybody, but main accepts 3 arguments:
int
main(int argc, char *argv[], char **env)

the env is set to environment.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!