Search code examples
creturnprogram-entry-point

Why doesn't main() function return float value?


I am writing a program in C that is supposed to return the float value 3.2058 after the function averagerOfFiveFloats() is called. The problem is when the code compiles and the IDE opens the executable file, the value returned is 1067450368. Why is this occurring? I made sure the variable types were correct and that both functions had a float return type.

The program takes five float values in the main function and then the function averagerOfFiveFloats() function is called within the main. The function averagerOfFiveFloats() takes the five float values, adds them together, divides the total by 5, and then returns the value.

Here is my code:

#include <stdio.h>

float a, b, c, d, e;

float averagerOfFiveFloats(float num1, float num2, float num3, float num4, 
float num5);

float main (void) {

    a = 1.25;

    b = 4.45;

    c = 3.45;

    d = 1.11;

    e = 5.769;

    return averagerOfFiveFloats(a, b, c, d, e);

}


float averagerOfFiveFloats(float num1, float num2, float num3, float num4, 
float num5) {

    float addition = 0;

    addition = num1 + num2 + num3 + num4 + num5;

    float divider = 0;

    divider = addition / 5;

    return divider;


}

Solution

  • The problem with your code is that you are attempting to return a float from main.

    The function main can return only an int.

    Instead you can print your result as

    printf("%f", averagerOfFiveFloats(a, b, c, d, e));
    

    and that produces the correct result.

    Also, the real average of your numbers cannot be 16.029 since all the numbers are less than that.