Menu

main function parameters

2004-09-10
2012-09-26
  • Nobody/Anonymous

    Can somebody please tell me how can you call function main, cause as I see there are parameters that can be passed (an int and an array).
    What is it used for? I hear it is for the command line, but no idea as to what effects it has on the program.

    Thanks in advance.

     
    • qWake

      qWake - 2004-09-10

      The parameters are not required.  You can define either:

      int main() ...

      or:

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

      You are talking about the second case.  The first parameter 'argc' tells you the number of string tokens present on the command line, so when you type something like this to start your program:

      myprogram parm1 parm2

      ...your program will start with argc equal to 3.  This means that you started it with three string tokens: the name of the program itself plus two parameters.

      The argv parameter is an array that contains three pointers to each of these tokens so that your program knows what its own name is as well as what the three parameters are.  You can print the value of these tokens using these pointers, so argv[0] points to the program name "myprogram", argv[1] points to "param1" and argv[2] points to "param2".

      It's up to you to do whatever you want with these values in your program.  You can also ignore them.

      qWake

       
    • Nobody/Anonymous

      Thanks!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.