ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
Code:
./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
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.
You can pass arguments to applications on the command line.
console commands like ls
ls -l
or
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.
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."
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
A very good source of help in c or c++ programming is the site Cprogramming.com. 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.