Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-26-2004, 06:42 PM   #1
Registered: Jun 2004
Location: United States
Distribution: slackware 10.0 mostly; used many
Posts: 109

Rep: Reputation: 15
main() confusioning in C

So if vars data-types are undeclared in C they are int? So int main()=main()--both work. My main question is what is the difference and what does the argc, and argv represent...look below for clarity.

int main() as opposed to:

int main(int argc, char *argv[]) as opposed to:

int main(int argc, char **argv)

Why use int argc and char *argv[] or char **argv?

Also, for exiting a program which is better?

int main() {


int main(){
return 0;

Old 07-26-2004, 07:15 PM   #2
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
argc represents the number of arguments passed to the program when it executes. argv is the array (or list) of arguments. So argc is the size of argv. As an example, lets say I created a program called mytest. Now I call it with the following arguments:

./mytest arg1 arg2 arg3
Now argc will equal 4. Why 4 you ask? Because the first argument is the program name itself 'mytest'. So then the contents of the argv array will be:

[mytest, arg1, arg2, arg3]

Like you said you can also make argv as a double pointer rather than an array. But since an array can be treated as a pointer in C, its technically the same thing. Only difference is you can't use indexing with the double pointer. And some programs don't need any arguments passed into them, which explains why you can have int main() without any arguments.

Does that make sense? I think I said everything right, but its been a while since I did any serious programming with program arguments so I might have botched something up.

As for your second question, I don't really think it matters. Personally I use return 0; whenever I can and only use exit(0); when I need to exit the program and the current scope of the program isn't in main

Last edited by R00ts; 07-26-2004 at 07:17 PM.
Old 07-26-2004, 07:16 PM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If you leave off the "int" return type for main, it defaults to an int. I don't know if that generalizes to all functions or not, but I know it applies to main(). However, just add it... you never know when you are going to come across a compiler that expects it and will croak if it's not there. It's just four simple characters.

In "int main()" you do not have access to argc and argv (to my knowledge). In other words, you know your program does not need or care about command-line arguments.

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

Both of the above are equivalent. They are just two different ways of saying the same thing.
argc = number of arguments on the command-line (including the program name itself)
argv = an array of strings representing the arguments on the command-line

My personal opinion is, always use "return 0" or some form of it. That's how functions are designed to work in C. Calling exit is a rather abrupt and "ungraceful" way of leaving the program.

Last edited by Dark_Helmet; 07-26-2004 at 07:36 PM.
Old 07-26-2004, 07:31 PM   #4
Registered: Aug 2002
Posts: 20,402

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
You can pass arguments to applications on the command line.
console commands like ls
ls -l
fdisk -l /dev/hda (fdisk is the program name and -l and /dev/hda are command line arguments)

These parameters are passed to main by the compiler startup routine. I believe that in linux you can use any valid variable name.

argc , an integer, is the number of command line arguments passed to main
argv is an array of pointers to strings char* []

argv[1] points to the first string typed after the program name, argv[2] is the second and so on. argv[0] points to the application name.

char ** and char* [] are different ways of saying the same thing.

Command line parameters are optional. If your application does not need them then just use main() instead.

Last edited by michaelk; 07-26-2004 at 07:32 PM.
Old 07-27-2004, 10:09 AM   #5
Registered: Jun 2004
Location: West Virginia
Distribution: Gentoo, Debian
Posts: 52

Rep: Reputation: 15
From a tutorial on C from the Univ. of Cambridge: "The default function type is `extern int' and the default type for the formal arguments is `int' but depending on these defaults is asking for trouble; they should be explicitly declared."
Old 07-27-2004, 10:33 AM   #6
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,781

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
exit is always ending the program while return is returning to the caller, so the effect is the same when used in the main function.
I prefer to call exit everytime, because I do not want to wonder if I am in the main or not, and it protects me from surprises if a piece of code is moved from the main to another place, or if the main method is renamed to sth different.

Concerning the char **argv vs char *argv[] question, the second form is just a facility to make the program easier (?) to read, but is anyway converted to the first form, as arrays are always passed by their 1st element address. I prefer the first form, as it is really what the main method get.
Old 07-27-2004, 11:27 PM   #7
Registered: May 2004
Distribution: Slackware.
Posts: 323

Rep: Reputation: 33
A very good source of help in c or c++ programming is the site The regulars to the site allways shoot down in flames void main(), void being the same in retrospect as just plain old main(). If you want to know why then the following will explain....

Personally I always go int main (void) or int main(). It remains portable (as far as I am aware) and saves the compiler you happen to be using at the time from shouting at you.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Greetings from Main g_trueblood Member Intro 1 09-15-2005 01:20 PM
./main to main DDD26 Linux - Newbie 3 07-25-2005 03:09 PM
(2) Main Menu's? Isn't it, "there can be only one?" for the term "Main"? t3gah LQ Suggestions & Feedback 1 04-09-2005 09:30 AM
more main()? linuxanswer Programming 3 10-26-2003 03:06 PM
help with gruger Linux - Software 3 06-20-2003 06:43 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration