我有2个变量,在我将这些数字输入计算机后,它们便为它们供电 喜欢:
我想这样做:a^b
。然后打印它:
int a ;
int b ;
scanf ("%d" , &a);
scanf ("%d" , &b);
答案 0 :(得分:1)
man pow
说:
双 pow(double x,double y);
... pow()函数计算x的乘幂y。
您需要包括math.h。 在代码中看起来像这样:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a, b;
if(scanf("%d", &a) != 1) {
fprintf(stderr, "wrong input for a");
exit(1);
}
if(scanf("%d", &b) != 1) {
fprintf(stderr, "wrong input for b");
exit(1);
}
double result = pow(a, b);
printf("result of %d^%d=%g\n", a, b, result);
return 0;
}
请注意,scanf
返回分配的输入项目数。因此检查那里的无效输入是有意义的。