Stabilo
Stabilo

Reputation: 269

sum and multiply of doubles and ints does not work

I wrote this code in C:

 double x1 = 7.52, x2 = 7, x3 = 8;
 int m = 0;
 double sum = x1 + x2*m + x3*m*m;

 printf("%lf", m, sum);

but sum is always 0 no matter what the value of m that i changed..

why doesnt it make normal calculation ?

thanks

Upvotes: 1

Views: 61

Answers (2)

KajolK
KajolK

Reputation: 193

Actually you are printing the value of m -

 printf("%lf", m, sum);  

You have to print -

 printf("%lf",sum);

Upvotes: 0

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

Because your are printing m which is int with the "%lf" specifier which is for double.

And you are also passing more arguments to printf() than format specifiers, which means that you are not enabling compiler warnings, you should, specially if you are a beginner.

Change this

printf("%lf", m, sum);

to

printf("m   = %d\nsum = %f\n", m, sum);

and see what I mean.

Upvotes: 3

Related Questions