HC08: Why sprintf does not work correctly ?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

HC08: Why sprintf does not work correctly ?

ソリューションへジャンプ
3,354件の閲覧回数
Theprophet
Contributor I
Hello,
 
 
What is wrong with that:
 
unsugned char str[70];
sprintf (str, "Offset = %0.3f\n",0.12345); 
 
But If I'm printing out str, I receive "Offset = 1.497" instead of "Offset = 0.123" !
In the past, as I remember, I already encountered such a problem and then I fixed it by casting of 0.12345 to (double)0.12345 because it states in CodeWarrior manual on sprintf page that "The argument must be a double".But now suddenly it is not working.
Double means 64 bit in my understanding so I tested in the "MMDS-MMEVS Settings" that double is indeed 64 bit rather than 32. There is a check box DOUBLE is IEEE32 and this option is not checked, so I assume double is actually 64.
If I compiling this code in an other C compler (Bloodshed DEV-C++ 4.9.9.2 ), it is working !
So how can I fix the problem ?
 
Thanks in advance to any replier.  
 
p.s. I work with HC08 3.1 build 4210
 

Message Edited by CrasyCat on 2007-04-13 11:46 AM

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
980件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee
check the readme.txt in lib\hc08c
The floating point format for the application and the ANSI library has to match.

Daniel

元の投稿で解決策を見る

0 件の賞賛
返信
5 返答(返信)
980件の閲覧回数
CrasyCat
Specialist III
Hello
 
Just tried it with a brand new project and I get it working.
Can you please check following points:
  - you should include St.h in the source file where you are doing the sprintf
  - which is  compiler options are you using?
    You can see the command line arguments in the following way:
       - Open target setting dialog (press ALT + F7)
       - Go to "Compiler for HC08" panel. What is written in Command Lines Arguments there?
  - Which ANSI library file are you linking to your application? If you are using 64-bit double, HCS08 and
    small memory model it should be ansis.lib.
CrasyCat
0 件の賞賛
返信
980件の閲覧回数
Theprophet
Contributor I
Dear Sir,
 
1) You say I should include St.h. Did you mean String.h ? String.h is included in the appropriate source file, otherwise I could not compile a code with uknown function.
2) The only compile option, I checked, is Memory Model (Small)
 
3) I have ansif.lib linked to my app. Should I replace it with ansis.lib ? If so, how can I do it ?
 
Best Regards.
0 件の賞賛
返信
980件の閲覧回数
CrasyCat
Specialist III
Hello
 
Oups spell checker did change the name of the file. You have to include stdio.h.
 
If you have only defined option -Ms, you have to link with ansi.lib.
Remove ansif.lib from the project and add the file ansi.lib (it is located in {Install}\lib\hc08c\lib directory)
 
CrasyCat
0 件の賞賛
返信
980件の閲覧回数
Theprophet
Contributor I
Dear CrazyCat,
 
Replacing ansif.lib with ansi.lib has indeed solved the problem. But why ?
I have never payd attention to this difference between ansi.lib and ansif.lib. Now I've checked my previous project, based on the same chip, and it also has ansif.lib included. So, I'd like to understand all the implications of this change, perhaps it can bring me some benefits, for example better code optimization etc.
Do you know where can I read about this issue ? In the HC08 manual I didn't find anything.
 
Thank you very much.
0 件の賞賛
返信
981件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee
check the readme.txt in lib\hc08c
The floating point format for the application and the ANSI library has to match.

Daniel
0 件の賞賛
返信