Search code examples
catoi

Why don’t the results of atoi() appear in my program’s output?


I’m trying to convert arguments passed on the command line to int using atoi, but it is taking forever no matter whether the string is small or big. Any ideas?

int main(int argc, char *argv[]) {
int id;
int v[5];
id=atoi(argv[2]);
v[0]=atoi(argv[3]);
v[1]=atoi(argv[4]);
v[2]=atoi(argv[5]);
v[3]=atoi(argv[6]);
v[4]=atoi(argv[7]);

//conversion must be taking forever; this is never printed
printf("%d %d %d %d %d %d", id,v[0],v[1],v[2],v[3],v[4]);

return 0;
}

Solution

  • I suspect you are Suffering from Buffering. Change your printf line to

    printf("%d %d %d %d %d %d\n", id,v[0],v[1],v[2],v[3],v[4]);
    

    Note the added newline, \n.