gpt4 book ai didi

c - 无法理解为什么我的代码中出现段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 16:50:15 25 4
gpt4 key购买 nike

这似乎是一个相对常见的问题,但其他帖子并没有为我指明该怎么做的正确方向。我很难看出这段代码为什么不起作用。

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

int main(){

double x,y,z;
double t = (x*x + y*y + z*z);
double s = sqrt(t);

printf("Component x: ");
scanf("%f, x");

printf("Component y: ");
scanf("%f, y");

printf("Component z: ");
scanf("%f, z");

printf("Magnitude: %f, s");

return 0;
}

最佳答案

您出现段错误,因为 scanf 的类型和变量都用双引号引起来,因此没有任何内容可读取。此外,对于 double ,“%f”应更改为“%lf”。

scanf("%f, x");

这应该是

scanf("%lf", &x);

在读取 z 值后,还要移动 t 和 s 计算语句。

..
printf("Component z: ");
scanf("%lf", &z);

double t = (x*x + y*y + z*z);
double s = sqrt(t);

printf("Magnitude: %lf" , s);
..

关于c - 无法理解为什么我的代码中出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com