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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • significantBit

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

    n00b here. Just started learning C a couple of days ago. I'm using
    xcode alternatively with emacs.

    My question is in regards to the main function.

    Everytime I create a project (standard command utility) with xcode, my
    main function starts out looking like this:

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

    What's all this in the parenthesis? Why use this rather than just
    main() ???
  • Chris Dollin

    #2
    Re: int main(int argc, const char * argv[]) ??

    significantBit wrote:
    n00b here. Just started learning C a couple of days ago. I'm using
    xcode alternatively with emacs.
    >
    My question is in regards to the main function.
    >
    Everytime I create a project (standard command utility) with xcode, my
    main function starts out looking like this:
    >
    int main(int argc, const char * argv[])
    >
    What's all this in the parenthesis? Why use this rather than just
    main() ???
    They're the arguments to the program. If you don't use that,
    the program doesn't get to see the arguments it was started
    with.

    [And really it should be `int main(void)`, not `main()`, if
    you really don't care about the arguments, prototypes being
    a Good Habit.]

    --
    "What would that matter, if it made a good book?" /Gaudy Night/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN

    Comment

    • John Bode

      #3
      Re: int main(int argc, const char * argv[]) ??

      On Apr 9, 7:40 am, significantBit <jfonte...@gmai l.comwrote:
      n00b here. Just started learning C a couple of days ago. I'm using
      xcode alternatively with emacs.
      >
      My question is in regards to the main function.
      >
      Everytime I create a project (standard command utility) with xcode, my
      main function starts out looking like this:
      >
      int main(int argc, const char * argv[])
      >
      What's all this in the parenthesis? Why use this rather than just
      main() ???
      If you want to be able to process arguments from the command line, you
      need these two parameters. argc contains the number of arguments on
      the command line (including the program name), and argv is the list of
      actual arguments (represented as character strings). For example, if
      you create a project foo and invoke it as follows:

      $ foo bar bletch

      argc will have the value 3 (there are three elements on the command
      line), and argv will contain the following:

      argv[0] = "foo"
      argv[1] = "bar"
      argv[2] = "bletch"

      If you don't intend to read any arguments from the command line, you
      can type main() as

      int main(void)

      That signature explicitly says that main() takes no arguments.

      Comment

      Working...