I have this program execute with the values 10,20,30 given at 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;
  }    
The outputs is The total number of arguments in the program is 4(null) 30 20 10 ./a.out
Where did that (null) come from ??
argv[0] is (to the extent possible) supposed to be something that identifies the program being run. argv[1] through argv[argc-1] are the arguments that were actually entered on the command line. argv[argc] is required to be a null pointer (§5.1.2.2.1/2).
argc is the total number of elements in the argv array; they are numbered from 0 to argc - 1. You are printing five values and only the last four are valid.
The way they taught you to count in school will not work in C. In C we count 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].
Basically you've got an off by one error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With