3 Replies - 3777 Views - Last Post: 03 February 2012 - 10:05 PM Rate Topic: -----

#1 akshayram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 12

char array to float conversion in c

Posted 03 February 2012 - 08:12 AM

i need help in converting a char array to a float datatype.
i have a char array a[10]={12.34} which i want to convert this array to float n=12.34 . i tried atof and i always get 0.00 as the answer.
Is This A Good Question/Topic? 0
  • +

Replies To: char array to float conversion in c

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: char array to float conversion in c

Posted 03 February 2012 - 08:17 AM

Please post your actual code that is giving you this problem so we can see exactly what is wrong.

From what you posted, a likely cause is that your char array isn't null-terminated. You should either declare it this way:

char a[] = "12.34";

or if you declare it your way, assign a null char '\0' to the array element following the last digit.
Was This Post Helpful? 1
  • +
  • -

#3 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: char array to float conversion in c

Posted 03 February 2012 - 02:58 PM

I'm fairly certain that if you define it as
char a[10]={12.34};
you'll just assign the first char as 12, which isn't even a number in the ASCII table, it's a control code that atof doesn't understand.

By assigning like
char a[10]="12.34";
you'll be assigning [0] the ASCII number 1, [1] the ASCII number 2, ect. atof will understand this format.

This post has been edited by OLH064: 03 February 2012 - 03:10 PM

Was This Post Helpful? 1
  • +
  • -

#4 akshayram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 12

Re: char array to float conversion in c

Posted 03 February 2012 - 10:05 PM

thank you guys for your help. i changed the array declaration as u suggested and it worked. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1