0
float f=44268/107402;
printf("\n%f",f);

output :

0.000000

How can this happen!

I am using pelles c ide on win 7.

alexandrul
  • 12,856
  • 13
  • 72
  • 99
Alex David
  • 585
  • 1
  • 11
  • 32

3 Answers3

5

The compiler treats the operands as integers. Try:

float f = 44268.0 / 107402;
                ^

Or maybe

float f = (float)44268 / 107402;
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thanks man.Now it returns 0.412171.But is still didn't get what's happening. It'll really help if can can you explain a bit? – Alex David Apr 25 '12 at 16:57
  • when you use an integer to dive an integer, the answer will be an integer too. The number after `.` will be "delete". @user1187786 – madper Apr 25 '12 at 17:03
2

Integer division truncates

float f=44268.0/107402;

Making one number float will automatically promote the other number ti float as well finally ending up in a floating point result.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
0
float f = 44268 / 107402;

The variable will be initialize with an integer value. You should add a floating point, to say to the compiler that you want a float value.

float f = 44268. / 107402.;
abelenky
  • 63,815
  • 23
  • 109
  • 159
md5
  • 23,373
  • 3
  • 44
  • 93
  • I really appreciate the explanation.Now i get it.Once again thanks to everyone for helping me, it really bugging for almost an hour. – Alex David Apr 25 '12 at 17:07