How to convert a string to integer in C?

后端 未结 12 1583
清酒与你
清酒与你 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:57

    In C++, you can use a such function:

    template 
    T to(const std::string & s)
    {
        std::istringstream stm(s);
        T result;
        stm >> result;
    
        if(stm.tellg() != s.size())
            throw error;
    
        return result;
    }
    

    This can help you to convert any string to any type such as float, int, double...

提交回复
热议问题