What are the arguments passed into the main method of a command-line program:
int main(int argc, const char * argv[])
what is the first int mea
The parameters to main()
are a unix convention for accessing the arguments given on the command line when your program is executed. In a Cocoa app, you can access them the plain old C way, or you can use NSProcessInfo
's -arguments
method to get them in an NSArray
of NSString
objects, or use NSUserDefaults
to get them as values in a dictionary.
That main is from C and not specific to objective-c. Argc gives you the number of command line arguments passed to your C program. Argv is an array of C strings and contains the command line arguments.
You would use them and the command-line project any time you wanted to write a command line tool or a program you interact with from the command line.
Also, what practical use is a command-line project type, other than using it to learn obj-c i.e. to practise.
The practical use is creating a command-line tool using code from a Framework or Application that you have written. Helpers, utilities, launch agents and daemons, all of these background processes are typically implemented as command-line tools.
argc
means "argument count". It signifies how many arguments are being passed into the executable.
argv
means "argument values". It is a pointer to an array of characters. Or to think about it in another way, it is an array of C strings (since C strings are just arrays of characters).
So if you have a program "foo" and execute it like this:
foo -bar baz -theAnswer 42
Then in your main()
function, argc
will be 5, and argv
will be:
argv[0] = "/full/path/to/foo";
argv[1] = "-bar";
argv[2] = "baz";
argv[3] = "-theAnswer";
argv[4] = "42";
Just to add to the other answers - Objective-C targets both OS X and iOS. And while there is not much value in iOS command line applications, the shell on OS X is still widely used and there are lot of people writing command line tools.
As wikipedia (and any other source says):
int main(void)
int main(int argc, char *argv[])
The parameters argc
, argument count, and argv
, argument vector, respectively give the number and value of the program's command-line arguments. The names of argc
and argv
may be any valid identifier in C, but it is common convention to use these names. In C++, the names are to be taken literally, and the "void
" in the parameter list is to be omitted, if strict conformance is desired. Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must stay int; for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:
int main(int argc, char **argv, char **envp)