gpt4 book ai didi

c - 这个简单的 C 程序有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:47 27 4
gpt4 key购买 nike

我正在学习 C,并在 Xcode 中试用程序。我在这里编写了这个简单的程序,并期望得到正确的输出。

//
// main.c
// Chap_1
//
// Created by Manish Giri on 6/17/14.
// Copyright (c) 2014 Manish Giri. All rights reserved.
//

#include <stdio.h>
#define PI 3.14159265

int main(int argc, const char * argv[])
{

// insert code here...
// printf("Hello, World!\n");

float radius, diameter, area, circumference;

printf("\n Enter the diameter of the circle\n");
scanf("%f",&diameter);

radius=diameter/2.0;

circumference= 2.0*PI*radius;
area=PI*(radius)*(radius);

printf("\n The area of the circle is: %.2f\n", area);
printf("\n The circumference of the circle is: %.2f\n", circumference);



return 0;
}

当我在 Xcode 中运行它并尝试提供此输入时 -

Enter the diameter of the circle
6

我从 Xcode 收到一个弹出窗口,说 - Chap_1 意外退出Lost Connection。然后,在输出 Pane 所在的屏幕底部,我看到这个 Program ended with exit code: -1

我在这里做错了什么?我没有发现这个程序有任何问题。是因为我一开始没有将我的变量初始化为零吗?如果这是原因,为什么需要它?

非常感谢您的帮助。

更新-

这是在 iMac 上的 Xcode(5.0.2) 上运行的程序的屏幕截图

screenshot

最佳答案

您的程序没有任何问题。 可能是在您输入内容并按下“Enter”键时出了点问题,scanf 无法正确解释它。类似的已知问题: Xcode: lost connection with Error code -1

从你的程序中删除 scanf 后,你能检查一次它是否给出同样的错误。

关于c - 这个简单的 C 程序有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24276718/

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