Arguments in the Main function

Jul 23, 2011 at 6:18am
I have seen arguments in the main function. But I couldn't find reference about them .
Could anyone explain me why there are arguments, and what do they do?
Jul 23, 2011 at 6:26am
I have also looked this up quite a few times. http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

However i think that (as a noob my self who has done a few months of programming) I could not understand the explanations given to me so it seems like one of those things you just got to wait untill you know about programming. :(
Jul 23, 2011 at 6:58am
Well, read through it, and it gives nothing about their use, and all that it does give goes above my head
Jul 23, 2011 at 9:00am
closed account (DSLq5Di1)
Command-line arguments. start->run, type 'notepad test', this will run notepad and pass "test" as an argument to notepads main function. Notepad attempts to open a file named test.txt.

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

argc is the number of arguments received. If I remember correctly the first argument is always the name of whatever executable you are launching, so argc should always be atleast 1. argv is an array of pointers to strings.

Assuming I created an app and launched it like so.. "someapplication.exe whats up doc!",

1
2
3
4
5
6
7
8
int main(int argc, char *argv[])
{
    for(int i = 0, i < argc; i++)
    {
        cout << argv[i] << endl
    }
    return 0;
}

someapplication.exe
whats
up
doc!
Last edited on Jul 23, 2011 at 9:01am
Jul 23, 2011 at 9:08am
Thanks, That is far easier to understand than the one in Wikipedia. Thanks a lot!
Topic archived. No new replies allowed.