top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to create floats from const char* in C?

0 votes
52 views

Trivial question, please help.

I need to convert a string literal like "111.25" to floating point number.
atof() fails me as it returns 1111 and discards .25?

posted Jul 28, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+2 votes

You must have forgotten to include the header file "stdlib.h" or maybe you used ""atoi"" by mistake instead of ""atof""

The below code works fine for me --

#include<stdio.h>
#include<stdlib.h>
main()
{
char *str="111.25";
float x;

x=atof(str);
printf("x=%f\n",x);
}

Output :

x=111.250000
answer Jul 29, 2015 by Ankush Surelia
Similar Questions
+3 votes

See the following code

void main(){
   float a=5.2;
  if(a==5.2)
     printf("Equal");
  else if(a<5.2)
     printf("Less than");
  else
     printf("Greater than"); 
}

Expected output is equal but we does not get same why?

+4 votes

I am wanting to extract variable names and their size (int, char and etc.) from a c file.
Is there any way to extract that type of information?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...