top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create floats from const char* in C?

0 votes
424 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 LinkedIn 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?

...