argc, argv???

Hi,

I have just started coding and am using MSVC++ express, I have spent a while trying to work out how you set argc and argv, when they are used as 'main' inputs to your program..i.e.

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

If anyone knows how this is done I would be gratefull for any help

Thanks in advance.. Tom




Answer this question

argc, argv???

  • djdarkblue

    Take a look at http://msdn.microsoft.com/library/default.asp url=/library/en-us/vccelng/htm/basic_21.asp for more details.

    Hope this helps!

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Asal

    TAG275 wrote:

    Hi,

    I have just started coding and am using MSVC++ express, I have spent a while trying to work out how you set argc and argv, when they are used as 'main' inputs to your program..i.e.

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

    If anyone knows how this is done I would be gratefull for any help

    What a wonderful question! I must remember to capture that in my material on console applications and Windows console sessions. Here's information in addition to what Ayman provided on how to make use of the information.

    When a console application (actually, any Windows application) is intiated from a command prompt, the command line that is used is passed to the application as string values pointed to from argv[]. For example,

    C:\>myprog arg1 "arg 2" arg3

    will cause myprog to be entered with

    argv[0] [ ] = "myprog" (not exactly - see below),

    argv[1] [ ] = "arg1"

    argv[2] [ ] = "arg 2"

    argv[3] [ ] = "arg3"

    argv[4] = NULL

    argc = 4

    You should try this with a little program that just displays what it receives when you call it from a command prompt.

    There are other ways to initiate your program, and this can lead to argv[0] being empty (a "\0" string) or something else unexpected. Also, I think that the standard Windows command shell puts in the full path of your program. Try it and see.

    Raymond Chen has a nice blog article on what can happen with other ways of initiating programs.

    - orcmid



  • Maran

    Thanks for getting back so quickly, the thing I wasnt sure about was actually on the command line itself, and how you run the program and define the inputs argc and argv from it which I havent been able to find anything about.. any ideas welcome!!

  • argc, argv???