atol() v/s. strtol()

前端 未结 7 1808
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 19:13

What is the difference between atol() & strtol()?

According to their man pages, they seem to have the same effect as well as matching arguments:

         


        
7条回答
  •  悲哀的现实
    2020-11-30 20:05

    If memory serves, strtol() has the added benefit to set the (optional) endptr to point to the first character that could not be converted. If NULL, it is ignored. That way if you're processing a string containing numbers and characters mixed, you could continue.

    e.g.,

    char buf[] = "213982 and the rest";
    char *theRest;
    long int num = strtol(buf, &theRest, 10);
    printf("%ld\n", num);    /* 213982 */
    printf("%s\n", theRest); /* " and the rest" */
    

提交回复
热议问题