Reputation: 1113
I have got a question from my junior and I can't fix it. Following is the code he is using in the Code::Blocks IDE just downloaded from official site of Code::Blocks.
It's a hello world console project, which he just modified a little, using the header file math.h
and using pow()
function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",pow(2,2));
return 0;
}
The output of this code should be Hello world! 4
right? But voila it's always Hello world! 0
unless I use printf("Hello world! %f\n", pow(2,2));
which is syntactically, yeah perfect and the right thing to do. But then it's another story altogether.
Pow function should return 4, double
, of course. So what is happening? printf()
is not working correctly, or there is some issue with pow()
.
Upvotes: 3
Views: 4964
Reputation: 459
pow(2,2)
will return a double always.
In printf()
%d and %i both are used for printing an int.If you use %d or %i for a double it will create problems.
In order to display a double use %f or %lf it will give you correct answer
printf("Hello world! %f\n",pow(2,2));
or
printf("Hello world! %lf\n",pow(2,2));
Upvotes: 0
Reputation: 153547
printf("Hello world! %d\n",pow(2,2));
"The output of this code should be Hello world! 4 right?"
No. As this is undefined behavior, anything can happen. As @Mureinik posted what likely happens in this errant situation, you could have a possible understanding of why you saw 0. But in the end, C does not have to perform this way.
... If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.. C11 §7.21.6.1 9
Casting to (int)
is a problem too.
1) int
range is much smaller than double
and for pow(2,100)
, that fails.
2) Casting to int
truncates the fractional portion on the result of double
and the preciseness of pow()
is not defined. This will given surprising result when the pow()
is something like 7.99999999999999
and 7
is printed rather than the hoped for 8.000000
.
If code needs an integer power function, consider unsigned long long int pow or https://stackoverflow.com/a/213897/2410359 or search around.
Upvotes: 1
Reputation: 311508
As you noted, pow
returns a double. printf
, however, has no awareness of the types passed to it in the stack, and if you pass a double
where an int
is expected, it will simply take the first sizeof(int)
bytes from the stack and interpret them as an int
.
E.g., on my machine (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) on x86_64), this program printed Hello world! -1954378952
.
If you want to treat the result as an int
by using %d
, you should explicitly cast it as such:
printf("Hello world! %d\n", (int)pow(2,2));
Upvotes: 1
Reputation: 59691
The return value of pow()
is double
as you can see here:
http://www.tutorialspoint.com/c_standard_library/c_function_pow.htm
So you have to cast the return value to int like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",(int)pow(2,2));
return 0;
}
Otherwise as you saw is the output 0!
Another example to show this you can try this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",4.000000);
return 0;
}
As you will see the output is also 0 because it's a double value!
Upvotes: 2