Harshwardhan Nandedkar
Harshwardhan Nandedkar

Reputation: 253

C: Float in argument, return type either giving a blank or an error

I was helping a junior out with C programming which i haven't done in over 4-5 years and i have forgotten quite a few syntaxes. Pasting below code

#include <stdio.h>
#include <conio.h>


######## Example 1 ########

divide(float x,float y)
{ 
    return (x/y);
}

void main()
{
int z;
z=divide(10.0,2.0);
printf("%d",z);

main.c:15:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 divide(float x,float y)
 ^~~~~~
main.c: In function ‘main’:
main.c:24:1: error: expected declaration or statement at end of input
 printf("%d",z);
 ^~~~~~

When i try with issuing an explicit return type as float in return(x/y)

######## Example 2 ########


divide(float x,float y)
{ 
    return (float)(x/y);
}

void main()
{
int z;
z=divide(10.0,2.0);
printf("%d",z);

}
main.c:15:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 divide(float x,float y)
 ^~~~~~
main.c: In function ‘main’:
main.c:24:1: error: expected declaration or statement at end of input
 printf("%d",z);
 ^~~~~~
######## Example 3 ########

divide(float x,float y)
{ 
    return (float) (x/y);
}

void main()
{
divide(10.0,2.0);

}
main.c:15:1: warning: return type defaults to ‘int’ [-Wimplicit-int]                                                                  


Program finished with exit code 5                                                                                                  
Press ENTER to exit console.       


The last code run returned a blank. I'm guessing it's returning a garbage value.
Can somebody please help me out with this?

https://www.onlinegdb.com/online_c++_compiler# I was using the above website to run my code if the compiler is of any help. Thanks!

Upvotes: 0

Views: 424

Answers (2)

pat
pat

Reputation: 185

I see three mistakes, first you need to specify the return value of the divide function float divide (float x, float y){ //function content}.

Secondly, since the return type of your function divide is a float, change int z; to float z; then go on with z=divide(10.0,2.0);

Thirdly, %d is for integers while %f is used for floats. Hence, instead of printf("%d", z); you have to use printf("%f", z);

Lastly, I have a suggestion. In general, if you can avoid dividing floats please do, just because of the error of overflows which can come out of it. For the purpose of this example it is fine cause the floats are small but it gets dangerous as the data gets larger. This is one of the best answer of how floats are stored in memory How are floating point numbers stored in memory?

Upvotes: 1

jmq
jmq

Reputation: 1591

You don't specify a return type for divide() so it defaults to a return type of int. Change it to:

float divide(float x,float y)

Also, you want to use printf() not print() and you are currently assigning the return value to an int. You'll want to assign it to a float and fix the printf format string.

float divide(float x,float y)
{ 
    return (float)(x/y);
}

void main()
{
    float z;   // You want z to be a float
    z=divide(10.0,2.0);
    printf("%f",z);  // Changed format to %f for float
}   

Upvotes: 2

Related Questions