How to convert a string using the function "atof"

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all!

Can some one please give me a simple example of "atof" function use.
I want to convert a string like this char s[]="11.1994"; to a float.

Your help is appreciated

Thanks,
Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
float f;
char s[]="11.1994";
f = atof(s);

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson,

Thanks for your input. I am going to try it.

Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I gotta ask - doesn't your C compiler have a manual? Did it not show an example of how to use it? How about your copy of K&R?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe he just doesn't own any of these books or manuals, but what really puzzles me is the fact that he's able to ask in a forum but unable to google for the info...

I fear these are just the signs of the times: instant satisfaction is what they are after, be it free advice for exactly their problem, or an instant meal at the drive-in of their favourite burger shop without even leaving the car (i was about to write "move their ass", but i guess this would be politically incorrect). But avoid like hell any effort...

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

Last Edited: Thu. Jan 19, 2012 - 08:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

True enough. My favourite C reference these days is now:

http://www.cplusplus.com/referen...

I almost never open a C manual any more because of that site.

The fact it is such a good/popular resource for C programmers is reflected in the fact that this is the very top hit if you Google only for the four letters "atof" !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could we include this picture at the very top of the avrfreaks web pages?

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I read in the http://www.cplusplus.com/referen... ... dlib/atof/ , I went to AVR C library and look into it, also, I look in a Portuguese C book. All I read was like the example gave to me, and I was doing the code as the example as well as the books, but, I was getting always a compiler error without mention the error type.

I asked just to make sure I was doing it the right way. I already solved the problem and was not related with the "atof", it was due to a wrong variable assignment type.

Clawson, your help was appreciated.

Thanks any way.
Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I was getting always a compiler error

Do you think it might not have been an idea to say that and quote that error here?

I bet I know what this is... just gazing into my crystal ball .... the smoke is clearing ... I see avr-gcc ... I see AVR Studio ... oh, wait a minute, I DON'T see libm.a ... oh dear.

It's very unwise to call any kind of floating point function in avr-gcc if your linker invocation does not include "-lm", which forces libm.a to be linked. Both AS4 and AS5 do not do this by default.

Of course this could be a completely different compiler or a completely different environment - my crystal ball has been on the fritz ever since one of the kittens knocked it off its stand!