C/C++ 中的默认 int 主参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17045493/
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-08-27 20:52:51  来源:igfitidea点击:

Default int main arguments in C/C++

c++c

提问by learner

I was messing around with projects in C/C++ and I noticed this:

我在处理 C/C++ 中的项目时,我注意到了这一点:

C++

C++

#include <iostream.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!\n";
    return 0;
}

and

C

C

#include <stdio.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

So I've always sort of wondered about this, what exactly do those default arguments do in C/C++ under int main? I know that the application will still compile without them, but what purpose do they serve?

所以我一直有点想知道,那些默认参数在 int main 下的 C/C++ 中究竟做了什么?我知道应用程序在没有它们的情况下仍然可以编译,但是它们的用途是什么?

回答by Kninnug

They hold the arguments passed to the program on the command line. For example, if I have program a.outand I invoke it thusly:

它们保存在命令行上传递给程序的参数。例如,如果我有程序a.out并且我这样调用它:

$ ./a.out arg1 arg2 

The contents of argvwill be an array of strings containing

的内容argv将是一个包含字符串的数组

  1. [0] "a.out"- The executable's file name is always the first element
  2. [1] "arg1"- The other arguments
  3. [2] "arg2"- that I passed
  1. [0] "a.out"- 可执行文件的文件名始终是第一个元素
  2. [1] "arg1"- 其他参数
  3. [2] "arg2"- 我通过了

argcholds the number of elements in argv(as in C you need another variable to know how many elements there are in an array, when passed to a function).

argc保存元素的数量argv(就像在 C中一样,当传递给函数时,您需要另一个变量来知道数组中有多少元素)。

You can try it yourself with this simple program:

你可以用这个简单的程序自己尝试一下:



C++

C++

#include <iostream>

int main(int argc, char * argv[]){
    int i;
    for(i = 0; i < argc; i++){
        std::cout << "Argument "<< i << " = " << argv[i] << std::endl;
    }
    return 0;
}


C

C

#include <stdio.h>

int main(int argc, char ** argv){
    int i;
    for(i = 0; i < argc; i++){
        printf("Argument %i = %s\n", i, argv[i]);
    }
    return 0;
}

回答by Santhosh Pai

If you want to accept arguments through commandline ,then you need to use the arguments in the main function .argc is the argument count and array of charecter pointers list the arguments. refer this link http://www.cprogramming.com/tutorial/c/lesson14.html

如果你想通过命令行接受参数,那么你需要使用主函数中的参数 .argc 是参数计数和字符指针数组列出参数。请参阅此链接http://www.cprogramming.com/tutorial/c/lesson14.html

回答by Zlatomir

argcand argvare how command line arguments are passed to main() in C and C++.

argcargv是命令行参数在 C 和 C++ 中传递给 main() 的方式。

argcwill be the number of strings pointed to by argv, this will usually be one more than the number of arguments you pass from terminal, as usually the first is the name of the program.

argc将是argv指向的字符串数,这通常比您从终端传递的参数数多一个,因为通常第一个是程序的名称。

回答by JAB

Those are for command-line arguments. argcis the number of arguments, and the arguments are stored as an array of null-terminated strings (argv). Normally, a program with no command-line arguments passed in will still have one stored in argv; namely, the name used to execute the program (which won't always be there, depending on how the program is executed, but I can't remember what the circumstances are for that).

这些用于命令行参数。argc是参数的数量,参数存储为以空字符结尾的字符串数组 ( argv)。通常,没有传入命令行参数的程序仍然会在argv; 中存储一个参数。即,用于执行程序的名称(它不会总是存在,取决于程序的执行方式,但我不记得是什么情况)。