Regarding 'main(int argc, char *argv[])'
Possible Duplicates:
What are the arguments to main() for?
What does int argc, char *argv[] mean?
Every program is starting with the main(int argc, char *argv[])
definition.
I don't understand what it means. I would be very glad if somebody could explain why we use these arguments if we don't use them in the program? Why not just: int main()
?
Is the name of the program one of the elements of *argv[]
and argc
is the count of the number of arguments in *argv[]
? What are the other arguments sent to *argv[]
? How do we send them?
The arguments argc
and argv
of main
is used as a way to send arguments to a program, the possibly most familiar way is to use the good ol' terminal where an user could type cat file
. Here the word cat
is a program that takes a file and outputs it to standard output ( stdout
).
The program receives the number of arguments in argc
and the vector of arguments in argv
, in the above the arg ument c ount would be two (The program name counts as the first argument) and the arg ument v ector would contain [ cat
, file
,null]. While the last element being a null-pointer.
Commonly, you would write it like this:
int // Specifies that type of variable the function returns.
// main() must return an integer
main ( int argc, char **argv ) {
// code
return 0; // Indicates that everything went well.
}
If your program does not require any arguments, it is equally valid to write a main
-function in the following fashion:
int main() {
// code
return 0; // Zero indicates success, while any
// Non-Zero value indicates a failure/error
}
In the early versions of the C language, there was no int
before main
as this was implied. Today, this is considered to be an error.
On POSIX -compliant systems (and Windows), there exists the possibility to use a third parameter char **envp
which contains a vector of the programs env ironment variables. Further variations of the argument list of the main
function exists, but I will not detail it here since it is non-standard.
Also, the naming of the variables is a convention and has no actual meaning. It is always a good idea to adhere to this so that you do not confuse others, but it would be equally valid to define main
as
int main(int c, char **v, char **e) {
// code
return 0;
}
And for your second question, there are several ways to send arguments to a program. I would recommend you to look at the exec*()
family of functions which is POSIX -standard, but it is probably easier to just use system
("command arg1 arg2")
, but the use of system()
is usually frowned upon as it is not guaranteed to work on every system. I have not tested it myself; but if there is no bash
, zsh
, or other shell installed on a *NIX-system, system()
will fail.
Those are for passing arguments to your program, for example from command line, when a program is invoked
$ gcc mysort.c -o mysort
$ mysort 2 8 9 1 4 5
Above, the program mysort
is executed with some command line parameters. Inside main( int argc, char * argv[])
, this would result in
Argument Count, argc = 7
since there are 7 arguments (counting the program), and
Argument Vector, argv[] = { "mysort", "2", "8", "9", "1", "4", "5" };
Following is a complete example.
$ cat mysort.c
#include <stdio.h>
int main( int argc, char * argv [] ) {
printf( "argc = %dn", argc );
for( int i = 0; i < argc; ++i ) {
printf( "argv[ %d ] = %sn", i, argv[ i ] );
}
}
$ gcc mysort.c -o mysort
$ ./mysort 2 8 9 1 4 5
argc = 7
argv[ 0 ] = ./mysort
argv[ 1 ] = 2
argv[ 2 ] = 8
argv[ 3 ] = 9
argv[ 4 ] = 1
argv[ 5 ] = 4
argv[ 6 ] = 5
[The char
strings "2", "8" etc. can be converted to number using some character to number conversion function, eg atol()
(link)]
With argc
(argument count) and argv
(argument vector) you can get the number and the values of passed arguments when your application has been launched.
This way you can use parameters (such as -version
) when your application is started to act a different way.
But you can also use int main(void)
as a prototype in C.
There is a third (less known and nonstandard) prototype with a third argument which is envp
. It contains environment variables.
Resources:
上一篇: 这些C的主要功能是什么?