How to convert a string to integer in C?

后端 未结 12 1578
清酒与你
清酒与你 2020-11-22 01:40

I am trying to find out if there is an alternative way of converting string to integer in C.

I regularly pattern the following in my code.

char s[] =         


        
12条回答
  •  时光取名叫无心
    2020-11-22 01:59

    //I think this way we could go :
    int my_atoi(const char* snum)
    {
     int nInt(0);
     int index(0);
     while(snum[index])
     {
        if(!nInt)
            nInt= ( (int) snum[index]) - 48;
        else
        {
            nInt = (nInt *= 10) + ((int) snum[index] - 48);
        }
        index++;
     }
     return(nInt);
    }
    
    int main()
    {
        printf("Returned number is: %d\n", my_atoi("676987"));
        return 0;
    }
    

提交回复
热议问题