C语言 关于'main(int argc, char *argv[])'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3898021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:38:49  来源:igfitidea点击:

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

cargvargc

提问by fuddin

Possible Duplicates:
What are the arguments to main() for?
What does int argc, char *argv[] mean?

可能的重复:
main() 的参数是什么?
int argc, char *argv[] 是什么意思?

Every program is starting with the main(int argc, char *argv[])definition.

每个程序都从main(int argc, char *argv[])定义开始。

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()?

我不明白这是什么意思。如果有人能解释为什么我们不在程序中使用这些参数,我会很高兴吗?为什么不只是:int main()

Is the name of the program one of the elements of *argv[]and argcis the count of the number of arguments in *argv[]? What are the other arguments sent to *argv[]? How do we send them?

程序的名称是否是 的元素之一,*argv[]并且argc是 中参数数量的计数*argv[]?其他参数发送给*argv[]什么?我们如何发送它们?

回答by Frank

The arguments argcand argvof mainis 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 catis a program that takes a file and outputs it to standard output (stdout).

参数argcargvofmain用作向程序发送参数的一种方式,可能最熟悉的方式是使用用户可以键入的好 ol' 终端cat file。这里的单词cat是一个程序,它接收一个文件并将其输出到标准输出 ( stdout)。

The program receives the number of arguments in argcand the vector of arguments in argv, in the above the argument count would be two (The program name counts as the first argument) and the argument vector would contain [cat,file,null]. While the last element being a null-pointer.

程序接收的参数的数目argc和参数向量中argv,在上述的精氨酸ument Ç'mount将两(程序名称计数作为第一个参数)和ARGument v厄克托将包含[ catfile]。而最后一个元素是空指针。

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:

如果您的程序不需要任何参数,main则以以下方式编写-function同样有效:

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 intbefore mainas this was implied. Today, this is considered to be an error.

在 C 语言的早期版本中,int之前没有main暗示过这一点。今天,这被认为是一个错误。

On POSIX-compliant systems (and Windows), there exists the possibility to use a third parameter char **envpwhich contains a vector of the programs environment variables. Further variations of the argument list of the mainfunction exists, but I will not detail it here since it is non-standard.

POSIX兼容的系统(和Windows),存在使用第三个参数可能char **envp包含的程序的载体ENVironment变量。该main函数的参数列表存在进一步的变化,但我不会在这里详细说明,因为它是非标准的。

Also, the naming of the variables is a conventionand 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 mainas

此外,变量的命名是约定俗成的,没有实际意义。坚持这一点总是一个好主意,这样你就不会混淆其他人,但定义main为同样有效

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 functionswhich is POSIX-standard, but it is probably easierto 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.

对于您的第二个问题,有多种方法可以向程序发送参数。我建议你看一下exec*()家庭的功能POSIX-标准,但它可能是更容易只使用,但使用时,它不能保证工作的每个系统上通常是皱起了眉头。我自己没有测试过;但如果没有,安装了* NIX系统上,或者其他的外壳,就会失败。system("command arg1 arg2")system()bashzshsystem()

回答by Arun

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 mysortis executed with some command line parameters. Inside main( int argc, char * argv[]), this would result in

上面,程序mysort是用一些命令行参数执行的。在里面main( int argc, char * argv[]),这将导致

Argument Count, argc = 7 

since there are 7 arguments (counting the program), and

因为有 7 个参数(计算程序),并且

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 = %d\n", argc );
    for( int i = 0; i < argc; ++i ) {
        printf( "argv[ %d ] = %s\n", 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 charstrings "2", "8" etc. can be converted to number using some character to number conversion function, e.g. atol()(link)]

[char字符串“2”、“8”等可以使用一些字符到数字的转换函数转换为数字,例如atol()(链接)]

回答by Colin Hebert

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.

使用argc(argument count) 和argv(argument vector),您可以在应用程序启动时获取传递参数的数量和值。

This way you can use parameters (such as -version) when your application is started to act a different way.

通过这种方式,您可以-version在应用程序以不同方式启动时使用参数(例如)。

But you can also use int main(void)as a prototype in C.

但是您也可以int main(void)在 C 中用作原型。

There is a third (less known and nonstandard) prototype with a third argument which is envp. It contains environment variables.

有第三个(鲜为人知且非标准的)原型,其第三个参数是envp. 它包含环境变量。



Resources:

资源:

回答by kraftan

argcmeans the number of argument that are passed to the program. char* argv[]are the passed arguments. argv[0]is always the program name itself. I'm not a 100% sure, but I think int main()is valid in C/C++.

argc表示传递给程序的参数数量。char* argv[]是传递的参数。argv[0]始终是程序名称本身。我不是 100% 肯定,但我认为int main()在 C/C++ 中是有效的。

回答by You

argcis the number of command line arguments given to the program at runtime, and argvis an array of arrays of characters (rather, an array of C-strings) containing these arguments. If you know you're not going to need the command line arguments, you can declare your main at taking a voidargument, instead:

argc是在运行时提供给程序的命令行参数的数量,并且argv是包含这些参数的字符数组(更确切地说是 C 字符串数组)。如果您知道不需要命令行参数,则可以在接受void参数时声明 main ,而不是:

int main(void) {
    /* ... */ 
}

Those are the only two prototypes defined for mainas per the standards, but some compilers allow a return type of voidas well. More on this on Wikipedia.

这是根据main标准定义的仅有的两个原型,但一些编译器也允许返回类型void在 Wikipedia 上有更多关于此的信息

回答by cschol

The comp.lang.c FAQdeals with the question

comp.lang.c常见问题与问题涉及

"What's the correct declaration of main()?"
Question 11.12a问题 11.12a

回答by Ondrej Slinták

You can run your application with parameters such as app -something -somethingelse. int argcrepresents number of these parameters and char *argv[]is an array with actual parameters being passed into your application. This way you can work with them inside of your application.

您可以使用诸如app -something -somethingelse. int argc表示这些参数的数量,并且char *argv[]是一个数组,其中包含传递到您的应用程序的实际参数。这样你就可以在你的应用程序中使用它们。

回答by Aliostad

argcis the number of command line arguments and argvis array of strings representing command line arguments.

argc是命令行参数的数量,argv是表示命令行参数的字符串数组。

This gives you the option to react to the arguments passed to the program. If you are expecting none, you might as well use int main.

这使您可以选择对传递给程序的参数做出反应。如果你不期待,你不妨使用int main.