We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
In one of my project I am using ARM cortex m3. Here I am trying to print a float variable value into a string variable ex. sprintf(sVar, "%f", 22.23); the value returned in sVar is never correct. But if I declare a float global variable, not even use this variable it starts printing value correctly into the sVar variable. Can anyone explain what might be going wrong here? THanks
using a float will pull the floating point libraries into the link. just using "%f" in a printf may not be sufficient to do so.
I understand that this might be happening in the background but I fail to understand why a local float defined does not solve this. It requires a global definition to resolve the issue. I have seen examples from Keil website for printf and that doesn't do anything different from what I've done. Can you suggest libraries that I should include manually to remove this such dependency because I tried few but that didn't solve the problem. Appreciate your help.
Note that a float is 32 bit large. So when you have an immediate float value as parameter, the compiler will most probably implement identical code as if it pushed a 32-bit integer. Nothing for the linker to see, to understand that floating point support is needed.
This is similar to the old Borland Turbo C days where "everyone" wondered why printf() wasn't able to print floating point numbers.
a local float (if it's unused) will simply get optimised away. so no need to include any support for it. a global is far less likely to get optimised out in this way; unless extreme global optimisation is carried out.
More importantly a local variable will have a random initialized value unless explicitly defined.
If you're printing something out, or calling another external routine with it as a parameter, it's not going to get optimized away