C语言 大神们帮我看看这个指数幂问题!不知道为什么我老是出错要求:(1) Prompt for and accept an unsigned short int value.(2) Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is t

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/25 15:00:52
C语言 大神们帮我看看这个指数幂问题!不知道为什么我老是出错要求:(1) Prompt for and accept an unsigned short int value.(2) Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is t

C语言 大神们帮我看看这个指数幂问题!不知道为什么我老是出错要求:(1) Prompt for and accept an unsigned short int value.(2) Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is t
C语言 大神们帮我看看这个指数幂问题!不知道为什么我老是出错
要求:
(1) Prompt for and accept an unsigned short int value.
(2) Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is the value from (1).
----------------------------------------------------------------------------------------------
输出例子:
Enter unsigned short int: 3
The value of 2 raised to the 3 power is 8
----------------------------------------------------------------------------------------------
我的代码:
#include
#include
#include
#include
int main()
{
short int number1, k;
printf("Enter unsigned short int:");
scanf("%d", &number1);
k = pow(2, number1);
printf("The value of 2 raised to the %d power is %d\n", number1, k);
return 0;
}

C语言 大神们帮我看看这个指数幂问题!不知道为什么我老是出错要求:(1) Prompt for and accept an unsigned short int value.(2) Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is t

这个问题是在于scanf通过%d输入数字的时候要求的指针类型是int型的.但是你输入了的是short型的指针,导致栈会被破坏,所以运行时会出错.把short int number1, k;声明里面的short去掉就好

至于pow调用的错误,进行显式类型转换就好pow((double)2, (int)number1);

不过这个错误其实很奇怪,因为c不存在函数重载的,函数重载是C++的概念,所以对于纯C来说是不存在这个问题的.我估计是你的编译器设置的时候没有设置成为根据源文件后缀来自动识别语言.所以他还是按照C++的方式来进行编译.这种显式类型转换虽然可以让他编译过去,但是实际上是按照C++的方式编译的,不是C的方式编译.你可以尝试下这个设置: