Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int main(int argc, const char * argv[]) ??

Reply
Thread Tools

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

 
 
significantBit
Guest
Posts: n/a
 
      04-09-2008
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() ???
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      04-09-2008
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

 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      04-09-2008
On Apr 9, 7:40 am, significantBit <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments