I just want to assign the value of pow to a variable, I've used this

#include<stdio.h>
#include<math.h>

int main( void )
{
int x;
int y;
int z;
x=10;
y=2;

z = pow(x,y);
printf ("%d" , &z);

return 0;
}

but in output I get -1076813284 , I am sorry, but I just started learning C and in every tutorial everyone just print the value of pow, like

printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));

and I don't know how to assign it to a variable

有帮助吗?

解决方案

printf ("%d" , &z);

prints the address of z (*), not its value. printf("%d", z) prints the value.

(*) Actually, the behavior is undefined and on a 64-bit CPU it will likely print half of the address.

其他提示

&z is the address of the variable z. If you want to print out the value of z, then the code is simply

printf("%d", z);

You would use &z when reading a value into z, because scanf needs a pointer in order to modify your variable.

pow returns a double (and not a reference), you need to make your print statement:

printf ("%f" , z);

After changing z to a double:

double z;

pow returns double and it takes arguments of type double.

double pow(double x, double y)  

You need %f specifier in printf and also remove & from z.

Another way is to cast the return value of pow to int and use %d

int z = (int)pow(x, y);
printf ("%d" , z);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top