I am trying to read an integer in from the command line, but it keeps reading in the ascii value as the int, and it completely blows up when the number is greater than 9. I know it's probably trivial but can anyone be of assistance please

int main(int argc, char*argv[]){

int x=(int)*argv[1];
int y=(int)*argv[2];
printf("x: %d y: %d\n",(int)x, y);


if the value is less than 10 it just prints the ascii value, otherwise it prints out the first ascii value of that argument

Any argument passed in the command line is going to be a string as you seems to know. However, casting it to just an int is not going to help. You need to convert that string numeric representation into an integer.

You could parse it using sscanf() or maybe convert using strtol()

Try converting the string to a long int first with strtol(): val = strtol( argv[1], NULL, 10 ); It's also a good idea to set errno=0 before calling the function; you can then check it afterward for errors (return value isn't reliable since 0 can be a valid result).

sorry johna i didn't try your so idk if works or not, but i was able to get it with sscanf from aia, thanks both of you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.