Operating System - OpenVMS
1752577 Members
5465 Online
108788 Solutions
New Discussion юеВ

Re: Incorrect float value returned from C function?

 
Kevin Iliff
New Member

Incorrect float value returned from C function?

Greetings,

I am attempting to write a unit test harness to test internal functions in a stand-alone c program.

Following are two pieces of simplified code to demonstrate my situation. First, the main program:

#include
#include
#include
extern float GetRatioMax()
{
float fResult;

fResult = 4.25;
printf("\nfloat value inside function: %f", fResult);
return(fResult);
}

/* Need the following #ifdef to link cleanly against the unit test harness. */
#ifdef UNIT_TEST_ON
test_uth()
#else
main()
#endif
{
/* Main left blank for simplicity. */
}

Now, the unit test harness:
#include
#include
#include
main()
{
float fRatio;
fRatio = GetRatioMax();
printf("\nValue after return: %f", fRatio);
}

Here are the results I get when I run the program on my system:
float value inside function: 4.250000
Value after return: 38.000000

I see the value I expect inside the function, but once the program returns, the value is incorrect. If I keep things in 1 module, it works correctly. If I pass the float as an argument, it works correctly. Anybody know why this is happening? We are running OpenVMS 7.3-2.

Thanks,

Kevin Iliff
6 REPLIES 6
Hein van den Heuvel
Honored Contributor

Re: Incorrect float value returned from C function?

Kevin,

Welcome to the OpenVMS itrc forum!

When compiling the 'main' as independent module, how does the compiler know GetRatioMax() is returning a float?

What are you not telling us, or what did I miss in your description.
Do you not get a warning like:

%CC-I-IMPLICITFUNC, In this statement, the identifier "GetRatioMax" is implicitly
declared as a function.

True, it's just a bunch of bits, so you coudl argue, but the compile uses floating point instructions which can re-interpret those bits. It will mov R0 (return) into the (stack) variable and then load that into a floating point register. It should do a floating point move (FMOV) from F0 to a selected target register, and optimize away the stack variable.

It would seem to me you need to define a function prototype somewhere down the line when compiling seperately.

When in doubt... compile with /LIST/MACHINE and compare the instructions for the working case vs the case where you lied to the compiler.

Hope this helps some!
Hein.
Steven Schweda
Honored Contributor

Re: Incorrect float value returned from C function?

This compiler complaint didn't speak to you?

alp $ cc c2

fRatio = GetRatioMax();
.........^
%CC-I-IMPLICITFUNC, In this statement, the identifier "GetRatioMax" is implicitl
y declared as a function.
at line number 7 in file ALP$DKA0:[SMS]C2.C;1


Try declaring GetRatioMax() in the unit test
harness:

#include
#include
#include

float GetRatioMax( void);

main()
{
float fRatio;
fRatio = GetRatioMax();
printf("\nValue after return: %f", fRatio);
}

Implicitly declared things are "int", not
"float".
Steven Schweda
Honored Contributor

Re: Incorrect float value returned from C function?

15:00:05, 15:00:58. That does it. I'm
through proofreading these things.
Kevin Iliff
New Member

Re: Incorrect float value returned from C function?

Of course. Adding the function prototype solved the problem. Thank you for your prompt assistance.

Kevin
Kevin Iliff
New Member

Re: Incorrect float value returned from C function?

As stated above, I added a function prototype to a header file that is included in both files, and the problem was solved.

I was not getting compiler warnings because the global cc symbol on our system suppresses this and many other warnings. Time for me to enable the warnings so I can see these things.

Once again, thanks for your help.

Kevin
Steven Schweda
Honored Contributor

Re: Incorrect float value returned from C function?

> [...] the global cc symbol on our system
> suppresses this and many other warnings.

I trust that the wisdom of that arrangement
has been made abundantly clear.