C++ scanf/printf的数组

Rad*_*mko 2 c++ arrays printf scanf

我写了以下代码:

int main() {
    double A[2];
    printf("Enter coordinates of the point (x,y):\n");
    scanf("%d,%d", &A[0], &A[1]);

    printf("Coordinates of the point: %d, %d", A[0], A[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它的行为如下:

输入点(x,y)的坐标:

3,5

该点的坐标:3,2673912

怎么可能,5转换成2673912?

And*_*ein 9

您正在使用十进制整数格式(%d)读取双精度值.尝试使用双格式(%lf)代替......

scanf("%lf,%lf", &A[0], &A[1])
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记`printf("点的坐标:%lf,%lf",A [0],A [1]); (3认同)