指数在C中无法正常工作

Jus*_*tin -2 c exponent

当我运行以下代码时

/*Program to find the greatest common divisor of two nonnegative integer
 values*/


#include <stdio.h>

int main(void){
    printf("    n  |  n^2\n");
    printf("-----------------\n");
    for(int n = 1; n<11; n++){
        int nSquared = n^2;
        printf("%i          %i\n",n,nSquared);
    }
}
Run Code Online (Sandbox Code Playgroud)

返回到终端的表显示如下

    n  |  n^2
-----------------
1          3
2          0
3          1
4          6
5          7
6          4
7          5
8          10
9          11
10          8
Run Code Online (Sandbox Code Playgroud)

为什么"n ^ 2"侧产生错误的数字?有没有办法在C中编写上标和下标,所以我不必显示"n ^ 2"并且可以将列的那一侧显示为"n²"而不是?

oua*_*uah 6

使用pow功能math.h.

^ 是按位异或运算符,与幂函数无关.