Akhil Raj
Akhil Raj

Reputation: 477

does printf dont take integer constant as arguement?

What's the problem with this code ?

   printf("%d", pow(2, 10));

as format specifier used is integral , it should give an integer value . But it don't . Why so ?

output - 0

Expected output - 1024

Upvotes: 0

Views: 73

Answers (3)

haccks
haccks

Reputation: 106012

pow return double. You are using wrong specifier to print a double value. This will invoke undefined behavior and you may get either expected or unexpected result. Use %f instead.

 printf("%f", pow(2, 10));

Upvotes: 5

Panagiotis Giannakis
Panagiotis Giannakis

Reputation: 400

cast to int

printf("%d", (int)pow(2, 10));

Upvotes: 1

M.M
M.M

Reputation: 141574

The pow function from <math.h> returns type double.

The format specifier for double is %f. You used the wrong specifier, so your program causes undefined behaviour. Literally anything could happen.

To fix this, use %f instead.

BTW if you want to compute 2 to the power 10 in integers you can write 1 << 10.

Upvotes: 4

Related Questions