c language + command line arguments _ command line arguments in C/C++

c language + command line parameters

In this article, we’ll be looking at how we can use command line arguments in C / C++.

In this article, we will look at how to use command line arguments in C/C++.

Command Line arguments are very useful if you want to pass any input strings to your main program, from the command line.

Command line arguments are useful if you want to pass any input string to the main program from the command line.

These arguments are passed as parameters to the main() function.

These parameters are passed as arguments to the main() function.

Let’s look at how we can use these effectively.

Let's see how to use them effectively.



Why use command line arguments? ( Why should we use command line arguments? )

Often, it is very convenient for us to directly give input to our program. One common way is to use scanf() / getchar(), etc to wait for a user input.

Often, it is very convenient for us to provide input directly to the program. A common approach is to use scanf()/ getchar()etc to wait for user input.

But, these calls waste a lot of time in waiting, and requires the user to manually enter the input.

However, these calls waste a lot of time waiting and require manual input from the user.

We can save a lot of time by simply giving these inputs to our main program!

By feeding these inputs to our main program, we can save a lot of time!

The format will be something like:

The format is as follows:


./executable input1 input2

The program will automatically store those command-line arguments in special variables, from which we can access them directly!

The program will automatically store these command line arguments in special variables from which we can directly access them!

This will only require a one time input, given when we start our program.

You only need to enter it once when you start the program.

Let’s look at how we can use them now.

Let's see how to use them now.

Command Line Arguments in C/C++ - The special variables

The program will pass the command line arguments to the main() function.

The program will pass command line arguments to the main()function.

In C / C++, the main() function takes in two additional parameters for these arguments.

在C / C ++中,main()函数为这些参数引入了两个附加参数。

  • argc -> Argument Count. Gives the number of arguments that we pass (includes the program name also)

    argc >参数计数。 给出我们传递的参数数量(还包括程序名称)
  • argv -> Argument Vector. This is a char* array of strings. These are the argument values itself.

    argv >参数向量。 这是一个char*字符串数组。 这些是参数值本身。

So, argv[0] is the name of the program itself, and argv[1]argv[argc-1] will be all our command line arguments.

因此, argv[0]是程序本身的名称,而argv[1]argv[argc-1]将是我们的所有命令行参数。


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

To see this in action, let’s take an example.

为了了解这一点,让我们举个例子。



使用命令行参数–一个简单的例子 (Using command line arguments – A simple example)

Let’s consider a program which concatenates two strings, given as input.

让我们考虑一个程序,该程序将两个字符串作为输入给出。

We’ll pass in two command line arguments to our program, so our total argc must be 3 (including the program name).

我们将两个命令行参数传递给我们的程序,因此我们的总argc必须为3(包括程序名)。

We can write our program like this:

我们可以这样编写程序:


#include <iostream> 
#include <string>

using namespace std;

string concat_strings(string s1, string s2) {
    return s1 + s2;
}

int main(int argc, char* argv[]) 
{ 
    cout << "You have entered " << argc 
         << " arguments:" << "\n";

    if (argc != 3) {
        cerr << "Program is of the form: " << argv[0] << " <inp1> <inp2>\n";
        return 1;
    } 

    string result = concat_strings(argv[1], argv[2]);

    cout << "Result: " << result << endl;

    return 0; 
} 

If our executable name was test.out, on my linux machine, I run the executable using this command:

如果我们的可执行文件名为test.out ,则在我的Linux机器上,我使用以下命令运行可执行文件:


./test.out Hello _JournalDev

Notice that the arguments are space-separated. So our command-line arguments are: “Hello” and “_JournalDev”

请注意,参数以空格分隔。 因此,我们的命令行参数为:“ Hello”和“ _JournalDev”

Output

输出量


You have entered 3 arguments:                                                                                                
Result: Hello_JournalDev

Great! This seems to work as expected, since the first argument is the name of the program itself.

大! 这似乎按预期工作,因为第一个参数是程序本身的名称。

Let’s try to run this with 4 arguments now.

现在让我们尝试使用4个参数运行它。


./test.out Hello from JournalDev

Output

输出量


You have entered 4 arguments:                                                                                                
Program is of the form: ./test.out <inp1> <inp2>

Indeed, it gives us the correct error message!

确实,它为我们提供了正确的错误消息!



结论 (Conclusion)

Hope this article gives you a better understanding of command line arguments. We saw how we can use it to make our lives easier!

希望本文能使您对命令行参数有更好的了解。 我们看到了如何使用它来使我们的生活更轻松!

For similar content, do go through our tutorial section on C++ programming.

对于类似的内容,请阅读我们有关C ++编程的教程部分

参考资料 (References)



翻译自: https://www.journaldev.com/41869/command-line-arguments-c-plus-plus

c语言+命令行参数

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324081799&siteId=291194637