Home > Blockchain >  Unable to pass float value in C
Unable to pass float value in C

Time:08-15

In my program, I have this requirement where I have to pass a float value back to the main function.

The code for the first approach is:

#include <stdio.h>
void main()
{
    int a,b;
    a=1;
    b=2;
    division(a,b);
}

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("%f",res);
}

The code for the second approach (where I pass the float value back to the main function) is this:

#include <stdio.h>
void main()
{
    int a,b;
    a=1;
    b=2;
    float res;
    res=division(a,b);
    printf("%f",res);

}

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    return res;
}

For the first approach, the output is 0.5, as expected. However, when I pass the result float (as shown in the next program), I am getting an output as 0. Why does this happen ? Is there any work-around for this ? I am relatively new to C (trying something apart from Java), so I am not very familiar with it's rules. When I modified int division to include a printf statement for variable res, like

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("Variable before passing %f ",res);
    return res;
}

I am getting the output as Variable before passing 0.500000 0.000000, which lead me to believe that the problem is with the passing of parameters back to the main function.

CodePudding user response:

There are actually two problems with the code and the division function:

  1. It's declared to return an int instead of a float. This leads to truncation in the conversion from float to int.

  2. The second problem is that you need to declare the function before you call it. C used to have implicit declarations where calling a previously undeclared function would make the compiler assume the arguments and return type of the function. One of the assumption would be that the function returned an int. And if you correct the division function you will then have it not match the implicit declaration by the compiler.

To solve both problems, I suggest you move the division function to be defined (and declared) before the main function, and using the correct types.

I also suggest you use double instead of float as types, and that you declare the arguments as such as well.

So perhaps something like this:

#include <stdio.h>

double division(double a, double b)
{
    // Exercise for reader: To protect against division by zero
    return a / b;
}

int main(void)
{
    int a = 1;
    int b = 2;
    printf("%f\n", division(a, b));
}

Note that I changed the return type of main from void to int. The main function is specified to always return an int. However you don't need an explicit return statement in the main function, if you leave it out the compiler will implicitly add a return 0; at the end.

CodePudding user response:

Well, thanks to the help of @Some programmer dude and @mikyll98, I was able to fix the code. The problem was that I had not done forward declaration and had put a wrong return type of int instead of float.

Corrected code:

#include <stdio.h>
float division(int a, int b);
void main()
{
    int a,b;
    a=1;
    b=2;
    float res;
    res=division(a,b);
    printf("%f",res);

}

float division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("Variable before passing %f ",res);
    return res;
}
  •  Tags:  
  • c
  • Related