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,

  1. always check return value of scanf(). without that, in case scanf("%f", &w); fails, you'll invoking undefined behavior you'll end using unitialized local variable float wk = w * wc; in float wk = w * wc;.

  2. please don't make return function. try stick return wk;.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -