C 编程语言问题中的函数:编程获得面积

问题描述

我是一名学生,正在尝试使用 C 语言编写一个简单的程序来使用函数获取三角形、正方形、矩形和圆形的面积。不幸的是,即使我仔细检查了语法并进行了调试,我的程序也不会运行。下面列出的是我的代码供参考:

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

float triangle(float b,float h)
{
    float triangle;
    triangle=0.5*b*h;
    return triangle;
}

float square(float s)
{
    float square;
    square=s*s;
    return square;
}

float rectangle(float l,float w)
{
    float rectangle;
    rectangle=l*w;
    return rectangle;
}

float circle(float r)
{
    float circle;
    circle=3.14*r*r;
    return circle;
}

main()
{
    float base,height,side,length,width,radius;
    
    printf("Triangle Area:\nEnter Base and Height:");
    scanf("%f%f",&base,&height);
    printf("Area of the Triangle= %f",triangle(base,height));
    
    printf("\nSquare Area:\nEnter Side:");
    scanf("%f",&side);
    printf("Area of the Square= %f",square(side));
        
    printf("\nRectangle Area:\nEnter Length and Width:");
    scanf("%f%f",&length,&width);
    printf("Area of the Rectangle= %f",rectangle(length,width));
    
    printf("\nCircle Area:\nEnter Radius:");
    scanf("%f",&radius);
    printf("Area of the Circle= %f",circle(radius));
    
return 0;
}

编辑:很抱歉没有按时回复你们。我遵循了您的建议之一,将 int main(void){} 而不是 main(){}。

我相信我的编译器有问题,因为当我尝试运行程序时,它只会显示编译结果,而且我的语法没有任何问题。我决定不输入 355.0 / 113.0 来替换 pi 的 3.14。我正在按要求使用 WindowsOS 平台。

我无法附上照片,因为我没有足够的声誉,但这是编译器中出现的内容

编译结果...

解决方法

您只需将 int(返回类型)放在 main() 之前作为函数定义的一部分。其他的好像都不错

int main(void)
{
    float base,height,side,length,width,radius;
    
    printf("Triangle Area:\nEnter Base and Height:");
    scanf("%f%f",&base,&height);
    printf("Area of the Triangle= %f",triangle(base,height));
    
    printf("\nSquare Area:\nEnter Side:");
    scanf("%f",&side);
    printf("Area of the Square= %f",square(side));
        
    printf("\nRectangle Area:\nEnter Length and Width:");
    scanf("%f%f",&length,&width);
    printf("Area of the Rectangle= %f",rectangle(length,width));
    
    printf("\nCircle Area:\nEnter Radius:");
    scanf("%f",&radius);
    printf("Area of the Circle= %f",circle(radius));
    
    return 0;
}
,

你只需要在 main 函数中添加一个类型。例如:int main(void) { … }