Argument for main()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alien
    New Member
    • Sep 2007
    • 61

    Argument for main()

    Hi,

    I am trying to get an understanding of main( ) functions with arguements.

    I read in a book where the code had some thing like this

    [code=c]
    int main(int argc, char *argv[])
    {
    /* Lines of codes in the body of main function */
    }
    [/code]

    Question is that main always have only this arguement? Can we make up something like this?

    [code=c]
    int main(int x)
    {
    printf("%d\n", x);
    }
    [/code]

    Regards,

    Alien
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    According to the C and C++ Standards main should have either no arguments; as in:

    [code=c]
    int main() { ... }
    [/code]

    or exactly two arguments as in:

    [code=c]
    int main(int argc, char** argv) { ... }
    [/code]

    Of course the names of the arguments can be different, but the first argument
    is an int and the second argument is a pointer to an array of char pointers, each
    pointing to one command line argument when the process was started.

    All others, and that includes the return type of the main function induce undefined
    behaviour which can be anything at all including the canonical deamons flying
    out of your nose ;-)

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      main() can only have these arguments:
      [code=c]
      int main(int argc, char *argv[])
      {

      }
      [/code]

      The reason is that this is the starting point of the program and the operating system has to know how to call your main(). The first argument is always the number of strings in the second argument and argv[0] is always the program name.

      By doing it this way, your main() can have unlimited arguments. They just appear as an array of strings. To use an int argument, it will appear as a string in the argv array. All you do is convert the string to an int and off you go.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        You have to realize that main is a very special function that gets called by the compiler crt. So you have to define the return type of main and arguments according to the C and C++ standards, or it's a bit difficult for the compiler to work with your code.

        main either takes no arguments, because you don't care about the command line arguments, or it takes an int and a char** argument to hold the parsed command line arguments. That's defined, and that's what the compiler expects. Do anything else, and either it won't compile outright, or your compiler will try and put something together, and you'll have completely undefined behavior as was said before.

        Comment

        • Alien
          New Member
          • Sep 2007
          • 61

          #5
          Thank you guys for the responses. It helped me a lot to understand.

          Kind Regards.

          Comment

          Working...