Take the 2-minute tour ×

Hi yes I am having some trouble with the following code. The function above executes and outputs 132.87 to the float variable but when I return this back to the main program the output is then shortened to 132.00

This is obviously something simple I am missing can anyone help me with this one please? Much appreciated.

calcSigmaXY(max) {
 int count= 0;
 float runTotal = 0, product[max];
 for (count = 1; count <= max; count++) {
     product[count] = pointData[count][1] * pointData[count][2];
     runTotal = runTotal + product[count];
 printf("\nruntotal is %4.2f",runTotal); // outputs 132.87

int maxPoints = 6;
float sigmaXY = 0,
sigmaXY = calcSigmaXY(maxPoints);
printf("\nsigmaxy set to : %4.2f\n", sigmaXY); // outputs 132.00
share|improve this question
I don't see the signature of calcSigmaXY(max), is this valid standard C code? Probably return type of the fuction is int so your return value gets converted. –  Jack Nov 26 '12 at 23:42
Thank you Jack, bit of a noob mistake on my part. –  Matthew Walton Nov 26 '12 at 23:43
@MatthewWalton I would recommend turning on warnings in your compiler, and making warnings be errors so you can't ignore them. If you're using GCC or Clang, you should be able to pass the flags -Wall -Wextra -Werror to turn on the most useful warnings and cause the compile to fail when you get a warning. I don't know what the MSVC equivalent is, but I'm sure there is one. That will help you avoid a lot of possible noob mistakes. –  Brian Campbell Nov 26 '12 at 23:46

2 Answers 2

You have to declare the return value of calcSigmaXY, otherwise the return value is int implicitly.

If you declare your function as float calcSigmaXY it should work as intended.

Also consider enabling and reading warnings in your compiler/IDE.

share|improve this answer

In some C versions, return values & types default to int, so by declaring your function like


you're basically saying

int calcSigmaXY(max) 

ergo the loss of precision - the float you return is converted to an int. Declare your function as

float calcSigmaXY(max) {
share|improve this answer

Your Answer


