argc and argv
2013-12-29 20:50
183 查看
So far, all the programs we have written can be run with a single command. For example, if we compile an executable called
we can run it from within the same directory with the following command at the GNU/Linux command line:
However, what if you want to pass information from the command line to the program you are running? Consider a more complex program like GCC. To compile the hypothetical
line:
The character strings
Command-line arguments are very useful. After all, C functions wouldn't be very useful if you couldn't ever pass arguments to them -- adding the ability to pass arguments to programs makes them that much more useful. In fact, all the arguments you pass on
the command line end up as arguments to the
Up until now, the skeletons we have used for our C programs have looked something like this:
From now on, our examples may look a bit more like this:
As you can see,
vector". A vector is a one-dimensional array, and
For example, the command line
would result in the following values internal to GCC:
The following program accepts any number of command-line arguments and prints them out:
If you name your executable
myprog,
we can run it from within the same directory with the following command at the GNU/Linux command line:
./myprog
However, what if you want to pass information from the command line to the program you are running? Consider a more complex program like GCC. To compile the hypothetical
myprogexecutable, we type something like the following at the command
line:
gcc -o myprog myprog.c
The character strings
-o,
myprog, and
myprog.care all arguments to the
gcccommand. (Technically
gccis an argument as well, as we shall see.)
Command-line arguments are very useful. After all, C functions wouldn't be very useful if you couldn't ever pass arguments to them -- adding the ability to pass arguments to programs makes them that much more useful. In fact, all the arguments you pass on
the command line end up as arguments to the
mainfunction in your program.
Up until now, the skeletons we have used for our C programs have looked something like this:
#include <stdio.h> int main() { return 0; }
From now on, our examples may look a bit more like this:
#include <stdio.h> int main (int argc, char *argv[]) { return 0; }
As you can see,
mainnow has arguments. The name of the variable
argcstands for "argument count";
argccontains the number of arguments passed to the program. The name of the variable
argvstands for "argument
vector". A vector is a one-dimensional array, and
argvis a one-dimensional array of strings. Each string is one of the arguments that was passed to the program.
For example, the command line
gcc -o myprog myprog.c
would result in the following values internal to GCC:
argc4
argv[0]
gcc
argv[1]
-o
argv[2]
myprog
argv[3]
myprog.cAs you can see, the first argument (
argv[0]) is the name by which the program was called, in this case
gcc. Thus, there will always be at least one argument to a program, and
argcwill always be at least 1.
The following program accepts any number of command-line arguments and prints them out:
#include <stdio.h> int main (int argc, char *argv[]) { int count; printf ("This program was called with \"%s\".\n",argv[0]); if (argc > 1) { for (count = 1; count < argc; count++) { printf("argv[%d] = %s\n", count, argv[count]); } } else { printf("The command had no other arguments.\n"); } return 0; }
If you name your executable
fubar, and call it with the command
./fubar a b c, it will print out the following text:
This program was called with "./fubar". argv[1] = a argv[2] = b argv[3] = c
相关文章推荐
- argc and argv
- !!!argc and argv as parameter of main function
- #Introduce about argc and *argv[]
- 调试console程序时,设置argc and argv
- argc and argv
- The meaning of "argc" and "argv" in programming
- C argc and argv Examples to Parse Command Line Arguments
- C argc and argv Examples to Parse Command Line Arguments
- C++ main函数中参数argc和argv含义及用法
- main()里的argc与argv
- main(int argc, char **argv)参数argc,argv说明和指向指针的指针变量
- MFC argc argv 如何获得命令行参数
- main 函数的两个参数argc、argv
- C/C++ 中的main函数的argc和argv参数
- argc和argv在main函数中的应用,及unistd.h
- 程序跳转到 mainret = main(argc, argv, envp); 抛出异常Unhandled exception
- C_argc与argv
- main函数中的argc和argv是什么意思?
- 看清main(argc,**argv)
- argc & argv的使用