floating point - C Program prints 0.00000 everytime but returns correct answer -
i have pretty simple code i'm having issues with. program calculates someone's weight in kilograms , displays it. however, every time run, printed answer comes 0.00000 returns correct answer. see wrong code?
#include <stdio.h> #include <math.h> int main(void) { float w; float const wc=0.454; printf("enter weight in pounds "); scanf("%f", &w); float wk = w * wc; printf("your weight in kilograms is: %f", &wk); return(wk); }
you don't need pass address of variable argument format specifier print value. need change
printf("your weight in kilograms is: %f", &wk); to
printf("your weight in kilograms is: %f", wk); that said,
always check return value of
scanf(). without that, in casescanf("%f", &w);fails, you'll invoking undefined behavior you'll end using unitialized local variablefloat wk = w * wc;infloat wk = w * wc;.please don't make
returnfunction. try stickreturn wk;.
Comments
Post a Comment