Re: Linux printf funny



Stef wrote:

Is this a win32 console application or an old DOS application?


It was W32 console, compiled with VC++6.


It does not sound like a windows/linux problem, but more like a
programming error. So please show us the code.


#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "WinTypes.h"
#include "ftd2xx.h"
#include "ADC.h"

int TareWeight = 0; // ADC reading at no- load
double ScaleFactor = 1.0; // Bridge scale factor

double ReadWeight(void)
{
char buff[100];
double fweight; // Float version of weight
int Weight; // Weight as returned by ADC

Weight= ADCConvert();

fweight= (double)(Weight - TareWeight) * ScaleFactor;
sprintf( buff, "%08x - %08x -> %9.3f", Weight, TareWeight, fweight);
MessageBox( 0,buff,"Conversion Result",0);
return fweight;
}



MessageBox() is just a function that displays the strings supplied, and is in fact a replacement for the Windows message box as in the program's earlier incarnation as a windows GUI program before it got converted to console (don't ask!).

Here's the console output:

Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087c0 - 000087bf -> 1.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087c0 - 000087bf -> 1.000
Conversion Result
000087bf - 000087bf -> nan
Conversion Result
000087c0 - 000087bf -> nan
Conversion Result
000087bf - 000087bf -> nan

"nan" then remains sticky until I restart the program. You'll notice that the int readings are much the same as when the output is correct!

I know I found some errors in old DOS (turbo C) test programs when I
ported them to new versions of compilers (windows or linux). These
programs worked normally using the old compiler but failed using a
newer one. All the errors where my own (beginners)fault: uninitialized
vars, wrong format specifiers, near/far pointer stuff etc.


I could believe that better if it didn't work for the first few readings.

Paul Burke

.


Quantcast