Convert char array to a int number in C

前端 未结 5 1080
梦毁少年i
梦毁少年i 2020-11-30 01:32

I want to convert a char array[] like:

char myarray[4] = {\'-\',\'1\',\'2\',\'3\'}; //where the - means it is negative

So it should be the

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 01:38

    Why not just use atoi? For example:

    char myarray[4] = {'-','1','2','3'};
    
    int i = atoi(myarray);
    
    printf("%d\n", i);
    

    Gives me, as expected:

    -123
    

    Update: why not - the character array is not null terminated. Doh!

提交回复
热议问题