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
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));
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
.
转换为int
printf("%d", (int)pow(2, 10));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.