ARGC/ARGV strangeness

Paul King pking123-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org
Tue Sep 20 00:33:56 UTC 2005


Hi

I have a little problem with a small program I have written.

I thought I had understood argc/argv under GNU C, but I guess this 
problem has me stumped.

I am writing a tiny program which takes an integer as argument,
and outputs its ASCII equivalent. Quite simple. Not more than 20 lines
of code. Here is the code:

---8<---snip----8<----------------

#include <stdio.h>
#include <strings.h>

int main (int argc, char *argv[]) {
   /* this only takes a number as an argument */
   int num = 0;
   int count = argc;
   for (; (count > 0); count--) {
         num = strtol(argv[count]);
         printf ("%d\t", count);
         printf ("%d\t'%c'\n", num, num);
   }
   return 0;
}

---8<---snip----8<----------------

The command line is this:

ascii 97 98 99 100 101

And the output is this:

6       0       ''
5       101     'e'
4       0       ''
3       99      'c'
2       0       ''
1       97      'a'

So you see my problem. 98 and 100 were apparently ignored, and then
to top it off, there is this "mystery parameter" #6 which shouldn't
be there at all. Why is every second parameter ignored, and why is the 
6th parameter there at all (I understand parameter 1 to be the 
program name)?

Paul King


--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list