C Command Line Arguments

I am running this program with the values ​​10,20,30 specified on the command line.

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;
  }    

Outputs The total number of arguments in the program is 4 (null) 30 20 10./a.out

Where did this (zero) come from?

+3
source share
7 answers

argv[0](as far as possible) should be something that identifies the program being executed. argv[1]through argv[argc-1]are the arguments that were actually entered on the command line. argv[argc]it is required to be a null pointer (§5.1.2.2.1 / 2).

+15
source

argc- total number of elements in the array argv; they are numbered from 0to argc - 1. You print five values, and only the last four are valid.

+5

, , C. C 0, 1, 2,...

+3

argv [4], argv [3], argv [2], argv [1], argv [0], argv [3], argv [2], argv [1], argv [0].

, .

+2

argc , argv[0] to argv[argc-1]. argv [argc-1].

, , , , . , .

0 . , , , . https://stackoverflow.com/questions/393462?tab=votes&page=1#tab-top

0

, , while(argc >= 0) , , . , argc argc-1.

, -, , argv [0] - , argc , , argc = 4, , ...

, , C , argv [argc] NULL, , , 1 - argc-1 ... ( )

0

(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]);
Hide result

C -

-1

Source: https://habr.com/ru/post/1757346/


All Articles