In the first example, radius has not been initialized before it's used. Its value is indeterminate. A good compiler with warnings turned on/up will let you know about this.
C is not Excel. In volume = 4.0f / 3.0f * 3.14159 * radius * radius * radius;, the value of the expression 4.0f / 3.0f * 3.14159 * radius * radius * radius is computed immediately, and not when the value of volume is accessed.
You also want to pass the value of volume to printf rather than its address. You've fixed this in the second example.
Best practices notes
You should be checking that scanf succeeded. Otherwise radius could still be uninitialized. A reasonable thing to do is to abort execution of the program, printing an error message to stderr.
Another option would be to print an error message and try again.
#include <stdio.h> int main(void) { float radius, volume; printf("Input your sphere's radius: "); if (scanf("%f", &radius) != 1) { fprintf(stderr, "Invalid input.\n"); return 1; } volume = 4.0f / 3.0f * 3.14159 * radius * radius * radius; printf("Here is its volume: %f", volume); return 0; }
And you probably want to print a newline after your output.
#include <stdio.h> int main(void) { float radius, volume; printf("Input your sphere's radius: "); if (scanf("%f", &radius) != 1) { fprintf(stderr, "Invalid input.\n"); return 1; } volume = 4.0f / 3.0f * 3.14159 * radius * radius * radius; printf("Here is its volume: %f\n", volume); return 0; }
There's very little point to using 32-bit floating point numbers with float when you can attain significantly more precision by using the 64-bit double type.
A function
You might implement a volume function that can take a radius as an argument and return the volume. This has the effect of calculating the volume on demand, more in line with your likely original expectation.
double volume(double radius) { return 4.0f / 3.0f * 3.14159 * radius * radius * radius; }
At which point your main function might be:
int main(void) { double radius printf("Input your sphere's radius: "); if (scanf("%lf", &radius) != 1) { fprintf(stderr, "Invalid input.\n"); return 1; } printf("Here is its volume: %lf\n", volume(radius)); return 0; }
printf("Here is its volume: %f", &volume);That ampersand ("address of variable") is required forscanf(), but wrong forprintf(). The former needs to know where to store the value; the latter only needs the value (not the address of the value.)volume =...is not a formula which will be calculated later - it is calculated at the moment and you have not entered value forradiusyet. And btw usedoublenotfloattype and constants withoutf.