Joel
Joel

Reputation: 55

C programming about "double" multiplication

I'm currently learning the C language and I'm having trouble in the double multiplication topic.

I need to to print the original value and then the 2*value of the double.

double num = 34.39;
printf("Original value = %d,   2x original value = %d", num, num*2);

How do I make it so that the 2x value will be really 2x the original value?

Upvotes: 1

Views: 5720

Answers (2)

Vadim
Vadim

Reputation: 41

%d - for int
You must use the "%f" for printf

Upvotes: 4

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

Your multiplication is not the problem.

Your printf format string is. %d is not for floating-point values, but for integers, so you're seeing nonsense resulting from your broken contract with the compiler.

double num = 34.39;
printf("Original value = %lf,   2x original value = %lf", num, num*2);

Upvotes: 8

Related Questions