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.
char array to float conversion in c
Page 1 of 13 Replies - 1762 Views - Last Post: 03 February 2012 - 10:05 PM
Replies To: char array to float conversion in c
#2
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:
or if you declare it your way, assign a null char '\0' to the array element following the last digit.
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.
#3
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
By assigning like
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
#4
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|