Hi everyone,
I had encounter some problem here ~ how can I convert double to string? I tried using sprintf but it seems that I encounter some problem, I wonder is it because I used mulit-thread application. any idea how I can over come the problems?
Hi everyone,
I had encounter some problem here ~ how can I convert double to string? I tried using sprintf but it seems that I encounter some problem, I wonder is it because I used mulit-thread application. any idea how I can over come the problems?
What some problem?
What's wrong with:
?Code:char s[32]; double d = 0.5; sprintf(s, "%Lf", d);
I'm not sure what the problem too. If I used the code that you gave me it works pretty fine if I used it in the temp workplace I created, but when I put it into my code it doesn't work. My code spawn a thread that called the following function, it gave me odd value, eg. "s = X&4" when I added your code into my function
The code I added to my function
char s[32];
double d = 0.5;
sprintf(s, "%Lf", d);
printf("s = %s \n",s);
%Lf is "long double", not double.
Just use "%f" for floats and doubles.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm still getting junk after I had changed it to %f. Is there anyway to go thru it?
Post your example and output.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I use this to test the program, it should be something like this :
Code:void Display_Handler () { char s[32]; double d = 0.5; sprintf(s, "%f", d); printf("Test print"); printf("s = %s \n",s); } void Update(void* temp) { while(repeat == TRUE){ /* Update */ Display_Handler(); Sleep(1000); } } void main(void) { _beginthread(Update, 0, NULL); while(true) { //do something something } }
my output = "s = "
I don't think there is any problem with my code, but I just couldn't get the string printed out. I do have a linker problem "LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other libs; use /NODEFAULTLIB:library" does it affect the outcome?
I don't see anything wrong there, but are you sure you want to say "nodefaultlib" to your linker? It may be that you are getting the single threaded libraries with this, and that's certainly not right in a multithreaded environment.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You certainly need to fix your linker warnings, and your void main.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm using visual studio 6.0 and I have set the run time library to Multi threaded DLL, did I miss somethings? what can I do so that I won't get this error message?
Excuse me, but I believe the correct conversion string for a double is "%lf", and not "%f".
Source: http://linux.die.net/man/3/scanf
Edit: never mind. printf's conversion strings seem to behave differently than scanf's.
Last edited by Mr_Miguel; 10-19-2007 at 03:49 AM.
Name: Miguel Martins
Date of birth: 14th August 1987
"He who hesitates is lost."