How can I get argv[] as int?

前端 未结 5 1864
离开以前
离开以前 2020-12-08 09:31

i have a piece of code like this:

int main (int argc, char *argv[]) 
{
   printf(\"%d\\t\",(int)argv[1]);
   printf(\"%s\\t\",(int)argv[1]);
}
5条回答
  •  臣服心动
    2020-12-08 10:07

    You can use strtol for that:

    long x;
    if (argc < 2)
        /* handle error */
    
    x = strtol(argv[1], NULL, 10);
    

    Alternatively, if you're using C99 or better you could explore strtoimax.

提交回复
热议问题