I'm trying to finish some basic coding exercise and I have already figured out how to fix it but I would just really like to understand what's going on.
#include <stdio.h>
#include <math.h>
int main(void) {
int first, second, third, fourth, fifth, sumOne, sumTwo, product;
printf("Enter integer 1: ");
scanf("%d", &first);
printf("Enter integer 2: ");
scanf("%d", &second);
printf("Enter integer 3: ");
scanf("%d", &third);
printf("Enter integer 4: ");
scanf("%d", &fourth);
printf("Enter integer 5: ");
scanf("%d", &fifth);
sumOne = first + second;
sumTwo = third + fourth;
printf("Result = %d", pow ((sumOne * sumTwo), fifth));
return 0;
}
I have been reading and I found out that pow provides a double result. I found that by changing the placeholder on the last printf() statement to %.0f, I get the desired result. Reason why I used %d is because I didn't want to get decimals in the output. I could also modify the data type and to double instead of int and just limit the decimals again on the final result.
I think my question is why have I been getting incorrect result with the use of %d as placeholder? Thank you.
The function
powis declared likeThat is its return type is double. So you need to write using the conversion specifier
finstead ofddouble numbers usually occupy 8 bytes (objects of the type int usually occupy 4 bytes) and have internal representation that differ from the internal representation of integers.
You could cast the result of the call of pow to the type
intand in this case use the conversion specifierd.Or you could round the result of the call of pow before outputting it as an integer.