I fail to understand why C outputs a value of 0.000 instead of 0.625. Shouldn't it convert the result of a/b automatically to a float? If I instead write: float a,b; the output is 0.625. Can you tell me where I get it wrong? Thank you.

#include <stdio.h>
#include <cs50.h>

int main(void)
int a,b;
float x = a/b;
printf("%0.3f", x);

This is because when a/b is performed the result is o form int type which is 0 since the .625 part of the result is gone due to the expression a/b being an int. When the value a/b (i.e 0) is assigned to float x it stores a value 0 as floating point number 0.000 and displays that. You can try the following line ( float x = (float) a / b ;)

  • Thank you Mahendra! – Alberto Mar 21 '16 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .