简体   繁体   English

C中的命令行参数

[英]Command line arguments in C

I have this program execute with the values 10,20,30 given at command line. 我让这个程序执行命令行给出的值10,20,30。

int main(int argc , char **argv)
 { 
  printf("\n Printing the arguments of a program \n");
  printf("\n The total number of arguments in the program is %d",argc);
   while(argc>=0)
    { 
     printf("%s   ",argv[argc]);
     argc--;
     }
     return 0;
  }    

The outputs is The total number of arguments in the program is 4(null) 30 20 10 ./a.out 输出是程序中的参数总数是4(null)30 20 10 ./a.out

Where did that (null) come from ?? 那个(null)来自哪里?

argv[0] is (to the extent possible) supposed to be something that identifies the program being run. argv[0] (尽可能)应该是识别正在运行的程序的东西。 argv[1] through argv[argc-1] are the arguments that were actually entered on the command line. argv[1]argv[argc-1]是实际在命令行输入的参数。 argv[argc] is required to be a null pointer (§5.1.2.2.1/2). argv[argc]必须是空指针(§5.1.2.2.1/ 2)。

argc is the total number of elements in the argv array; argcargv数组中元素的总数; they are numbered from 0 to argc - 1 . 它们的编号从0argc - 1 You are printing five values and only the last four are valid. 您正在打印五个值,只有最后四个有效。

他们教你在学校计算的方式不适用于C.在C中我们算0,1,2,......

Because you're printing out argv[4], argv[3], argv[2], argv[1], argv[0], instead of argv[3], argv[2], argv[1], argv[0]. 因为你打印出argv [4],argv [3],argv [2],argv [1],argv [0],而不是argv [3],argv [2],argv [1],argv [ 0]。

Basically you've got an off by one error. 基本上你有一个错误。

argc will have number of elements which can be accessed from argv[0] to argv[argc-1] . argc将具有多个元素,可以从argv[0] to argv[argc-1] So modify your condition accordingly viz print from argv[argc-1]. 因此,请相应地修改您的条件,即从argv [argc-1]打印。

Here is a command line arguments tutorial link as there are many things which you may have missed when reading it. 是一个命令行参数教程链接,因为在阅读它时可能会遗漏许多内容。 Hence you are not able to understand the reason for that output. 因此,您无法理解输出的原因。

Numbering for indexes is usually from 0, because of many reasons. 由于许多原因,索引的编号通常为0。 Please check this question which will help you understand why its zero based. 请检查这个问题,这将有助于您理解为什么它基于零。 https://stackoverflow.com/questions/393462?tab=votes&page=1#tab-top https://stackoverflow.com/questions/393462?tab=votes&page=1#tab-top

I think that the fact that the code is while(argc >= 0) shows that the you know that arrays are zero indexed. 我认为代码是while(argc >= 0)的事实表明你知道数组是零索引的。 The problem is that you start at argc instead of argc-1. 问题是你从argc而不是argc-1开始。

or, put another way, you appear to understand that argv[0] is the name of the program, argc INCLUDES that as an argument, so when it says argc = 4, it means that there are 3 arguments in addition to the program name... 或者换句话说,你似乎明白argv [0]是程序的名称,argc包含它作为参数,所以当它说argc = 4时,它意味着除了程序名称之外还有3个参数...

And, as Jerry Coffin pointed out, C requires the argv[argc] to be NULL, as a sentinal, in case knowing that the arguments are 1 - argc-1 isn't enough... (Belt and suspenders) 并且,正如Jerry Coffin指出的那样,C要求argv [argc]为NULL,作为一个sentinal,以防知道参数为1 - argc-1是不够的......(腰带和吊带)

It is giving output as (null) 30 20 10 because you are using argv[argc], and when you pass 10,20,30 then number of arguments is 4 (as the first argument is always program name) so for the first time argv[argc] means argv[4] ie, fifth argument (as array begins with a[0] therefore argv[0] will be the first argument ...argv 1 will be the second....so on.....argv[4] will be the fifth argument passed) which is null therefore it is printing as (null) 30 20 10. For the proper result use 它输出为(null)30 20 10,因为你正在使用argv [argc],当你传递10,20,30时,参数的数量是4(因为第一个参数总是程序名)所以这是第一次argv [argc]表示argv [4]即第五个参数(因为数组以[0]开头,因此argv [0]将是第一个参数... argv 1将是第二个....所以...... ..argv [4]将是传递的第五个参数),因为它打印为(null)30 20 10.为了正确的结果使用

 printf("%s ",argv[argc-1]); 

Command Line Argument in C Programming - Concept C编程中的命令行参数 - 概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM