Ok, I have a char that is a number. How could I convert it into a double?
char c;
I’ve tried (double)c, but it converts to zero.
Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
if you have a null terminated string that you wish to convert to double use
atof:If you have a single character, casting should work:
If the character is zero then it print zeros of course:
Note:
atofand similar functions don’t detect overflows and return zero on error, so there’s no way to know if it failed (not sure if it setserrno), see also Keith’s comments about undefined behaviour for certain values, so the point is you should usestrtolfor converting from strings tointandstrtodfor converting todoublethose have much better error handling: