J Pi
J Pi

Reputation: 41

All decimals are removed after calling a function using double

printf is removing the decimal places of my numbers. In my case both 6.9 and 6.4 print as 6.0.

#include <stdio.h>
#include <stdlib.h>


int main()
{
    double Value1_m, Value2_m;
    double maximum, minimum;
    printf("Enter two decimal numbers with an space: ");
    scanf("%lf" "%lf", &Value1_m ,&Value2_m);
    //printf("%3lf", Value1_m);

    maximum = ComputeMaximum(Value1_m, Value2_m);
    minimum = ComputeMinimum(Value1_m, Value2_m);

    printf("ComputeMaximum %.10lf\n", maximum);
    printf("ComputeMinimum %.10lf", minimum);
    return 0;
}

ComputeMaximum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value1 : Value2;

}
ComputeMinimum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value2 : Value1;

}

Upvotes: 0

Views: 79

Answers (2)

John Bollinger
John Bollinger

Reputation: 181199

You have not declared return types for your ComputeMaximum and ComputeMinimum functions. This is erroneous in modern C, and your compiler should be at least warning you. Moreover, you are calling those functions at a point where the have no in-scope declarations. This, too, is erroneous.

However, it used to be that if functions declared without a type were implicitly declared to return int, and that functions without in-scope declarations were assumed to return int, too. A compiler that falls back to that behavior will accept your program, but the return values of those functions will be truncated to type int, and then the result converted back to double when you assign the values.

First, declare your function's desired return type.

Second, declare your functions before they are called, either by putting the complete definitions first, or by providing forward declarations:

double ComputMaximum(double, double);
double ComputMinimum(double, double);

// ...

Be sure that any forward declarations agree with the function definitions.

Upvotes: 1

rassa45
rassa45

Reputation: 3560

Printf is not the problem here, the problem is your functions themselves.

Look at this portion

ComputeMaximum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value1 : Value2;

}
ComputeMinimum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value2 : Value1;

}

You don't have a return type in these methods, meaning that the compiler doesn't know what's going to be given back with any finality. Whatever you are using is defaulting to returning an int, which as you have stated will result in getting rid of numbers after the decimal.

So essentially, the compiler reads it as this, adding a default return type.

int ComputeMaximum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value1 : Value2;

}
int ComputeMinimum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value2 : Value1;

}

You need to add a return type to these methods explicitly, so the default doesn't come into play.

double ComputeMaximum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value1 : Value2;

}
double ComputeMinimum(double Value1, double Value2)
{

    return  (Value1 > Value2) ? Value2 : Value1;

}

Upvotes: 0

Related Questions