C++ atoi(argv[1]) 的结果是什么?

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

What will be the result of atoi(argv[1])?

c++

提问by Adeel

I just want to know what these lines actually do.

我只想知道这些行实际上是做什么的。

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

And especially this one:

尤其是这个:

int n = atoi (argv[1]);

I read this in a book but I cannot understand these lines.

我在一本书上读到了这个,但我无法理解这些行。

回答by dasblinkenlight

This converts the first command-line argument to an integer. For example, if you call your program like this

这会将第一个命令行参数转换为整数。例如,如果你这样调用你的程序

./a.out 123

then nwill be 123.

然后n将是123

Note that before accessing argv[1]one must check that argcis greater than 1, i.e. check that at least one argument has been passed to your program on a command line.

请注意,在访问之前argv[1]必须检查它argc是否大于1,即检查是否至少有一个参数已通过命令行传递给您的程序。

回答by Youn Elan

argc is the count of argument. argv is short for argument variable. It will contain all arguments passed on the command line. argv[1] contains the first argument so atoi(argv[1]) will convert the first argument to an int

argc 是参数的计数。argv 是参数变量的缩写。它将包含在命令行上传递的所有参数。argv[1] 包含第一个参数,因此 atoi(argv[1]) 会将第一个参数转换为 int