c - How to cast program argument *char as int correctly? -


i'm on mac os x 10.6.5 using xcode 3.2.1 64-bit build c command line tool build configuration of 10.6 | debug | x86_64. want pass positive integer argument, i'm casting index 1 of argv int. seems work, except seems program getting ascii value instead of reading whole char array , converting int value. when enter 'progname 10', tells me i've entered 49, when enter 'progname 1'. how can c read entire char array int value? google showed me (int)*charpointer, doesn't work.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>  int main(int argc, char **argv) {     char *programname = getprogramname(argv[0]); // gets name of executable     if (argc < 2) {         showusage(programname);         return 0;     }     int thenumber = (int)*argv[1];     printf("entered [%d]", thenumber);            // entering 10 or 1 outputs [49], entering 9 outputs [57]     if (thenumber % 2 != 0 || thenumber < 1) {         showusage(programname);         return 0;     }      ...  } 

the numer in argv array in string representation. need convert integer.

sscanf

int num; sscanf (argv[1],"%d",&num); 

or atoi() (if available)


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -