0

I am currently learning c , when I use a function with return using float it always gives output as zero after decimal point. eg: output is 523.000000 but it should have been 523.333333

when I change every variable to float, it gives the below when float is used for all variableerror.

#include <stdio.h>


  main ()
{ 
   float r,ans;
    printf("r=");
    scanf("%f",&r);
    ans =volume(r);
    printf("volume of sphere is %f",ans);
    return 0;
 
}
 volume(float x)
 { 
  float v;
  v= (4/3.0)*(3.14)*x*x*x;
  return(v);
 } 
`#include <stdio.h>

// when int is used for r
  main ()
{  int r;
   float ans;
    printf("r=");
    scanf("%d",&r);
    ans =volume(r);
    printf("volume of sphere is %f",ans);
    return 0;
 
}
 volume(int x)
 { 
  float v;
  v= (4/3.0)*(3.14)*x*x*x;
  return[output when int is used for r variable](https://i.stack.imgur.com/f6KwJ.png)(v);
 } 
  • https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – kotatsuyaki Dec 13 '22 at 16:13
  • 1
    Just what do you think `volume(int x)` returns? – Andrew Henle Dec 13 '22 at 16:14
  • [The `main` function](https://en.cppreference.com/w/c/language/main_function) must always be declared to return an `int`, explicitly. You must always declare symbols (like function) before you use them. With a valid return type. Whatever resource you're using to learn C must either be very old, or very bad. – Some programmer dude Dec 13 '22 at 16:15
  • 1
    If what you're learning from doesn't have return types on all its functions, switch to a difference source (one from this century would be a good idea). – Mat Dec 13 '22 at 16:15
  • 2
    By default the return value from functions is `int`. Declare volume() as "float volume(int x)" (specify the return value). Also, the parameter to volume() is also an int, but you are scanf-ing a float - so you may want to change that as well – ChrisSc Dec 13 '22 at 16:16
  • Your learning resource is outdated. ```main()``` is an incorrect definition of ```main```. It's either ```int main(void)``` or ```int main(int argc, char *argv[])``` – Harith Dec 13 '22 at 16:18
  • @Haris C17 specifies `int main(void)`, `int main(int argc, char *argv[])` or equivalent; or _in some other implementation-defined manner_. So more than 2 options may be available with current C. – chux - Reinstate Monica Dec 13 '22 at 16:24

1 Answers1

0

All you need to do is state the return type, like so: float volume(float x).

As mentioned in the comments, functions need a specified return type (what kind of value they should return, or void if they return nothing). For example, float foo() { ... } is fine but foo() { ... } is not. The latter will default to returning an int for compatibility, but a compiler will probably complain.

Function volume is declared without a return type, so the compiler assumes it returns an int, and complains (the image you attached shows the complaint).

The reason your program prints 523.000000 is because you force v to be converted to an int when you return it from function volume, losing the precision after the decimal. When stored in ans, another conversion takes place to turn it back into a float, but the precision has already been lost.

Here's everything put together for extra clarity.

#include <stdio.h>

float volume(float x) {
    float v;
    v = (4/3.0)*(3.14)*x*x*x;
    return v;
}

int main() {
    float r, ans;
    printf("r=");
    scanf("%f", &r);
    ans = volume(r);
    printf("volume of sphere is %f\n", ans);
    return 0;
}

Good luck learning C!

nathaniel
  • 98
  • 7