468,512 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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() ???
Apr 9 '08 #1
2 11210
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

Apr 9 '08 #2
On Apr 9, 7:40 am, significantBit <jfonte...@gmail.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.
Apr 9 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Steven T. Hatton | last post: by
13 posts views Thread by Sokar | last post: by
9 posts views Thread by pkirk25 | last post: by
3 posts views Thread by Bill Cunningham | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.