atof function.

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

#include
#include

int main()
{
float f;
while(1)
{
f=atof("156");

}
return 0;
}

Can someone plz help me.. When I debug this code, I always get "No source available".. I have to add "libm.a"..? If yes, someone plz tell me how to add this in studio5.?

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

What do you expect?

It should work with or without libm.a

Seriously, you could have just said:

int main()
{
    float f = 156.0;
    while(1)
    {
    }
    return 0;
}

You will not be able to step through the atof() library function source code. (Unless you build the whole library yourself from source)

You can add libm.a by selecting your project options and the Libraries item in the Linker section.

You might have noticed that your program does not actually do anything. The optimiser will most likely remove the code completely !

David.

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

Hi David,
I try to write a simple code for converting a string to float.. I also try something like..

int main(void)
{
char x[10] = "4.641592";
double a = atof(x);
}
But, during debugging the value in "a" remains '0'.. plz help me..

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

Quote:

I also try something like..

int main(void)
{
   char x[10] = "4.641592";
   double a = atof(x);
} 


Again, and as David pointed out, the program does not do anything meaningful. The optimizer likely throws the whole contents of the main function out.

You need to read this: https://www.avrfreaks.net/index.p...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Well, I thought I should try it for myself.

int main(void)
{
   char x[10] = "4.641592";
   volatile double a = atof(x);
} 

with the 'volatile' and different Optimisation settings:
1. with -O0 you can see both x[] and a
2. with -O1 you can see both x[] and a
3. with -Os you can see both x[] and a
without the 'volatile':
5. with -O0 you can see both x[] and a
6. with -O1 you can see x[] but not a
7. with -Os you can see x[] but not a

You really need to think up some better test programs.
Incidentally, I was using Studio4.18 and WinAvr.

Studio5 should do something similar.

David.

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

david.prentice wrote:

Well, I thought I should try it for myself.

int main(void)
{
   char x[10] = "4.641592";
   volatile double a = atof(x);
} 

with the 'volatile' and different Optimisation settings:
1. with -O0 you can see both x[] and a
2. with -O1 you can see both x[] and a
3. with -Os you can see both x[] and a
without the 'volatile':
5. with -O0 you can see both x[] and a
6. with -O1 you can see x[] but not a
7. with -Os you can see x[] but not a

You really need to think up some better test programs.
Incidentally, I was using Studio4.18 and WinAvr.

Studio5 should do something similar.

David.

 

You are not addressing why atof() doesn't work --  I have he same problem and nothing added to the linker options makes atof or sscanf(%f) work.  

 

 

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

david.prentice wrote:

Well, I thought I should try it for myself.

int main(void)
{
   char x[10] = "4.641592";
   volatile double a = atof(x);
} 

with the 'volatile' and different Optimisation settings:
1. with -O0 you can see both x[] and a
2. with -O1 you can see both x[] and a
3. with -Os you can see both x[] and a
without the 'volatile':
5. with -O0 you can see both x[] and a
6. with -O1 you can see x[] but not a
7. with -Os you can see x[] but not a

You really need to think up some better test programs.
Incidentally, I was using Studio4.18 and WinAvr.

Studio5 should do something similar.

David.

 

You are not addressing why atof() doesn't work --  I have he same problem and nothing added to the linker options makes atof or sscanf(%f) work.  

 

 

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

GaryStofer wrote:
You are not addressing why atof() doesn't work

But they have addressed why the whole program does not "work" (sic):

 

You really need to think up some better test programs

It has nothing specifically to do with atof() - it is a general fact that trivial programs which do nothing will get optimised to nothing

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>> But they have addressed why the whole program does not "work" (sic):

 

 

Indeed that they have brilliantly --- I just wonder how many times I have to write my own atof /sscanf function --  It's 2015 people. This should not be a problem anymore.    ..

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

You're missing the point again!

 

It's nothing to do with atof() - it is a general fact that trivial programs which do nothing will get optimised to nothing

 

Therefore,

 

 

  •  

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As Andy says atof() works perfectly if you use it in a valid program. You underestimate the compiler. It is very smart and will spot when you are just "messing about" and it will "eat" your test program.

 

To defeat that either use volatile inputs/outputs or just turn optimization off all together (-O0) - though this latter option can have dire consequences elsewhere and volatile is a better solution.

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

clawson wrote:

As Andy says atof() works perfectly if you use it in a valid program...

 

Do you really think I'm that dumb to not check that the program actually calls atof() and that there is a variable that actually is used before I post something? 

 

The person who gave the example above years ago probably cut out a few lines he thought where significant to show the problem to the forum . Then trampling all over him for not giving a completely fool proof sample doesn't help his problem or mine.

 

Anyways -- In ASudio 6.2 , on a 1281  with -lm and  the various  extra libs  as per ATEML docs and others atof() and strtod() returns the number 16249 while sscanf( mychar*,"%f",&tmp_flt);  never touches tmp_flt.

 

If  anybody knows what's the hidden secret to actually getting a string to float conversion working with the standard C library features then speak up, otherwise don't bother giving wise ass advice and wasting everyone's time.

 

 

 

 

 

 

 

 

 

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

Test program? Also what is the expected result and actual result?

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

atof() will work perfectly with any compiler and any "settings".    and you include < stdlib.h>

scanf("%lf"") will only work if the "big" floating point library is linked.   and you include < stdio.h>

 

Bear in mind that you have hooked onto a 3 year old thread from someone that cannot spell "please".

I am quite happy with answering a new poster because it is clearly the "same" question.   However we do need to have some idea about your knowledge / experience to reply appropriately.

 

David.

Last Edited: Fri. Jul 24, 2015 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have he same problem

No you don't. You are merely making the (wrong) assumption that atof and scanf are related to each other.

Regards,
Steve A.

The Board helps those that help themselves.

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

david.prentice wrote:

......   and you include < stdlib.h>

 

Solved -- Missed <stdlib.h>

 

Dis not see the warning flashing by  saying that atof() was used without a function prototype and did not have "All Warnings as Error" set to fail the compile.

 

I recall that other compilers I use regularly will consider a missing prototype as an error by default.

 

Gary

 

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

I recall that other compilers I use regularly will consider a missing prototype as an error by default.

 

Really?  I didn't realize that standard C >>required<< prototypes.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.