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

查看:24
本文介绍了关于'main(int argc, char *argv[])'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

程序的名称是*argv[]的元素之一,argc*argv[]中的参数个数?发送到 *argv[] 的其他参数是什么?我们如何发送它们?

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?

推荐答案

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

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).

程序接收argc中的参数个数和argv中的参数向量,在上面的argumentcount 将是两个(程序名称算作第一个参数)并且 argument vector 将包含 [cat,file,null].而最后一个元素是一个空指针.

The program receives the number of arguments in argc and 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.

通常,你会这样写:

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.
}

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

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
}

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

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.

POSIX 兼容系统(和 Windows)上,存在使用第三个参数 char **envp 的可能性,它包含程序的向量 环境熨烫变量.main 函数的参数列表的其他变体存在,但我不会在这里详细说明,因为它是非标准的.

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

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

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;
}

对于您的第二个问题,有多种方法可以向程序发送参数.我建议您查看 exec*() 函数系列 这是 POSIX 标准,但使用 system("command arg1 arg2"),但是system()的使用是通常不赞成,因为它不能保证在每个系统上都能工作.我自己没有测试过;但是如果在 *NIX 系统上没有安装 bashzsh 或其他 shell,system() 将失败.

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.

这篇关于关于'main(int argc, char *argv[])'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆