简体   繁体   中英

Putting value of a char array into a float variable

I want to put the value of an array into a float integer.

main(){
    float a;
    char array[4]="12.1";
    a=atoi(array);
    printf("%f",a);
}

When I uses this program, it gives 12.000000 as output but I want 12.100000 as output. Thanks in advance.

Use of this :

atof() — Convert Character String to Float :

#include <stdlib.h>
double atof(const char *string);

This link explains about that.

Summarizing the answers and comments, your program should look like:

int main(void) {
    float a;
    char array[]="12.1";
    a=atof(array);
    printf("%f\n",a);
}

代替将字符数组转换为整数的atoi () ,请使用atof()阅读此处

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