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

Discussion in 'C Programming' started by significantBit, Apr 9, 2008.

  1. 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() ???
    significantBit, Apr 9, 2008
    #1
    1. Advertising

  2. significantBit

    Chris Dollin Guest

    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
    Chris Dollin, Apr 9, 2008
    #2
    1. Advertising

  3. significantBit

    John Bode Guest

    On Apr 9, 7:40 am, 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() ???


    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.
    John Bode, Apr 9, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinu
    Replies:
    9
    Views:
    588
  2. Vinu
    Replies:
    4
    Views:
    4,780
    Ron Natalie
    May 10, 2005
  3. Hal Styli
    Replies:
    14
    Views:
    1,609
    Old Wolf
    Jan 20, 2004
  4. mlt
    Replies:
    4
    Views:
    668
    James Kanze
    Jun 9, 2009
  5. lkcl
    Replies:
    1
    Views:
    645
    Stefan Behnel
    Jul 1, 2010
Loading...

Share This Page