Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use command line parameters in C language

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use C language command line parameters", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language command line parameters how to use" article.

The command line arguments in C language are very simple and only need a simple example to illustrate:

# include void main (int argc,char** argv) {printf ("% d", argc); printf ("% s", argv [0]); printf ("% s", argv [1]); printf ("% s", argv [2]);}

In the above example, we pass two arguments to the main function, argc and argv. Argc is of type int, which represents the number of command line arguments. Do not ask the user to pass, it will be automatically determined based on the number of parameters entered by the user from the command line. Argv is of type char**, and its purpose is to store parameters passed in by the user from the command line. Its first member is the name of the program the user is running.

For the above example, we save it as test.c and compile it with gcc to generate the object file as test. The test is as follows:

(1) We run the program directly under the command line without passing any other parameters:

. / test

The running results are as follows:

The first output is argc, because we only entered. / test, so argc is 1, that is, there is only one command line argument. The first command-line argument output later is also. / test. Then the program went wrong, because there are no second and third parameters, so when writing the actual application, we should pay attention to the fault-tolerant processing of this part.

(2) We enter at the command line:

. / test hello world

You can see that argc is 3, the first parameter is. / test, the second parameter is hello, and the third parameter is world.

(3) We continue to enter:

. / test hello world hello world

You can see that at this point, the argc becomes 5gargv index and gives the first three parameters, and of course you can output the next two parameters as well.

If a parameter passed on the command line includes spaces, you need to expand the parameter with "" for example

. / test "hello world"hello world"

If we pass the hello world as a whole as a parameter, we need to use "hello world".

Note that the type of argv can be char* * argv,char argv [] [], char* argv [].

The above is about the content of this article on "how to use C language command line parameters". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report