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

S

significantBit

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() ???
 
C

Chris Dollin

significantBit said:
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.]
 
J

John Bode

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top