-2

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.

Saurabh
  • 43
  • 1
  • 15

3 Answers3

5

Use of this :

atof() — Convert Character String to Float :

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

This link explains about that.

machine_1
  • 4,266
  • 2
  • 21
  • 42
3

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);
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

instead of atoi () which converts character array to integer, use atof() read here