c - 将多个参数传递给 main()

标签 c parameters command-line-arguments program-entry-point

对于我的类(class),我需要创建一个在运行时接收多个参数的函数:

void main(int x, int y, int generation, char *layout[20])

然而,当程序使用我对这些变量的输入运行时,信息不会存储在调试过程中

run 3 3 3 Test_Round
print x // returns 5
print y // returns -8779

如何将多个参数传递给 main 函数,以便 main 能够识别我给它的参数?

谢谢大家!我只是使用 argv[1] 等来获取我需要的正确数据!

最佳答案

标准清楚地说明了您需要如何声明您的主要功能。

来自 C99 标准:

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

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

or with two parameters

(referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ } 

or equivalent;9) or in some other implementation-defined manner.

If they are declared, the parameters to the main function shall obey the following constraints:

— The value of argc shall be nonnegative.

argv[argc] shall be a null pointer.

— If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

关于c - 将多个参数传递给 main(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912147/

相关文章:

c - C 中用于有符号整数结构 num { int a :3; int b:2; int c:1; } 的位字段

c++ - 使用 C API 的有序 lua 表循环

c - 如何在 char 变量上传递参数并打印它

python - 在python中使用命令行参数将变量设置为c程序的输出

c - 当 strdup 功能失败时?

c - c程序中特定格式的系统时间

java - JPA 标准 API。使用带有参数的sql函数调用查询

c# - 为什么我们不能在派生类中使用带参数的构造函数

c++ - C++ 不将 `std::vector<std::string>` "overload"作为参数添加到 `main()` 的原因是什么?

c - 处理命令行参数?