ARGC/ARGV strangeness

Steven Woon stevenwoon-D1t3LT1mScs at public.gmane.org
Tue Sep 20 02:46:35 UTC 2005


Try this:

-----
#include <stdio.h>
//#include <strings.h>
#include <stdlib.h>

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

-----

Best regards,
Steven


On Mon, 2005-09-19 at 21:37 -0400, Marcus Brubaker wrote:
> A few things:
> 
> First, count should be initialized to argc-1 as argv is 0 indexed like 
> all other C arrays.
> 
> Also, I suspect that strtol is running into a space somewhere and 
> getting confused.  To confirm this, print out the arguments as you parse 
> them (i.e., do a printf("%s-\n",argv[count]))  The dash at the end of 
> the line is to see if there are trailing whitespaces.  It also seems 
> like there may be some oddness in whatever you're using to call the 
> program as most shells will eliminate those extra spaces.
> 
> Regards,
> 
> Paul King wrote:
> 
> >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
> >  
> >
> 
> --
> 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

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