简体   繁体   中英

how to convert char array to integer in C?

How can I convert the char array x to an integer 89 in the code below? Thank you

int main(int argc,char *argv[]){
    char y[13] = "0123456789012";
    char x[3];
    int integer_value;

    x[0] = y[8];
    x[1] = y[9];
    x[3] = '\0';

    integer_value=atoi(x);
}

You're done; atoi() is one way of doing the conversion from a string to an integer. You could also use strtol() or sscanf() .

UPDATE: Assuming, of course, that you fix the termination, ie set x[2] = '\\0'; rather than x[3] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM