如何将命令行参数传递给 c 程序

2021-12-27 00:00:00 c command-line c++

自从我学会编程以来,我就知道如何编写一个接受命令行参数的程序.我不明白的是这些参数如何获得它们的值.希望我没有将这两者混为一谈,但是参数和参数之间是有区别的.argument 是函数被调用时赋予的值,例如: foo( a, b, c);其中 a、b 和 c 是值.参数是调用时函数内部的值.

I've known how to write a program that accepts command line arguments ever since I learned to program. What I don't understand is how these parameters get their values. Hopefully I don't have these two mixed up but there is a difference between an argument and a parameter. An argument is the value given to the function when it is called such as: foo( a, b, c); where a, b, and c are the values. A parameter is the values that are inside the function while is being called.

所以我的问题是一个人如何将命令行参数传递给程序?我了解如何读取参数,argc 是参数的数量,argv 是指向包含参数等的字符串数组的指针,等等,但我只是不知道如何给这些参数赋值..

So my question is how does a person pass command line arguments to a program? I understand how to read the arguments, that argc is the number of arguments, argv is a pointer to an array of strings containing the arguments, etc. etc. but I just don't know how to give those arguments a value..

我正在寻找有关 C 和 C++ 的信息.我是这方面的新手.

I'm looking for information for both C and C++. I'm sort of a novice at this.

推荐答案

在 Windows 环境中,您只需像这样在命令行上传递它们:

In a Windows environment you just pass them on the command line like so:

myProgram.exe arg1 arg2 arg3

argv[1] 包含 arg1 等

argv[1] contain arg1 etc

主要功能如下:

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

相关文章