for rahul sir

2 views
Skip to first unread message

tarang gupta

unread,
Nov 18, 2009, 9:37:10 AM11/18/09
to CSL 101 group 7 2009
sir the programme given for command line arguments on the lecture
notes at saroj mam's webpage for file handling given on the last slide
is not understandable
pls mail an explanation for it

Rahul Gupta

unread,
Nov 18, 2009, 10:18:51 AM11/18/09
to csl-101-gr...@googlegroups.com

The code given is

"

main(int argc,char *argv[])
{
  while(argc>0)      /* print out all arguments in reverse order*/
    {
      printf("%s\n",argv[argc-1]);
      argc--;
    }
}


"

1) argc stores the number of elements which would be there in the array argv

2) So just considering argv as an array with number of elements (= argc), how will you print the elements of that array in reverse order ??

Array argv has elements from i=0 till i=argc-1
You would start from the end of the array ie.from argv[argc-1] ..print the value stored at that location and decrement i.

The above program can be rewritten as

for (i=argc-1;i>0;i--)
printf("%s",argv[i];

Compare both the programs and understand how both of them are essentially the same only.



2009/11/18 tarang gupta <tara...@gmail.com>

tarang gupta

unread,
Nov 18, 2009, 11:42:12 AM11/18/09
to csl-101-gr...@googlegroups.com
but sir the main problem is the output
will you please show how the programme would run on putting the set of
values taken in the slide

Rahul Gupta

unread,
Nov 18, 2009, 11:46:26 AM11/18/09
to csl-101-gr...@googlegroups.com
The ouput as shown is
"
$ cc args.c -o args.out
$ ./args.out 2  join leave 6
6
leave
join
2
./args.out
"

* argc =5
* What are the various values of the elements present is argv ?

argv[0]= ./args.out
argv[1] = 2
argv[2] = join
argv[3] = leave
argv[4] = 6


So the program will print from argv[4] till argv[0]

I hope I am making sense.


Rahul



2009/11/18 tarang gupta <tara...@gmail.com>

tarang gupta

unread,
Nov 18, 2009, 12:24:26 PM11/18/09
to csl-101-gr...@googlegroups.com
sir but where in the programme did we specify that argc is 5?

Rahul Gupta

unread,
Nov 18, 2009, 12:26:57 PM11/18/09
to csl-101-gr...@googlegroups.com
 It automatically computes the value of argc looking at the number of parameters passed. If you would pass one more argument, it would automatically increase the value of argc to 6.

The value of argc need not be passed.

tarang gupta

unread,
Nov 18, 2009, 12:38:34 PM11/18/09
to csl-101-gr...@googlegroups.com
so whatever we pass after the file name gets stored in argv and the
'2' is certainly not the value of argc passed?

Rahul Gupta

unread,
Nov 18, 2009, 12:44:12 PM11/18/09
to csl-101-gr...@googlegroups.com
This '2' has nothing to do with the value of argc. Argc depends on how many arguments you enter. The moment you stop entering the arguments, the variable argc acquires its value by counting the number of arguments passed by you

2009/11/18 tarang gupta <tara...@gmail.com>
Reply all
Reply to author
Forward
0 new messages