简体   繁体   中英

Trying to write a Program to calculate the Circumference & area of a circle in C

So I tried to write a program to tell me the circumference and area of a circle when I input the radius but it don't want to work ( I am following instructions from a book (Beginning C by Ivor Horton's)) so here is the program ;

#include <stdio.h>

void main()
{
    float radius= 0.0f;
    float circu= 0.0f;
    float area= 0.0f;
    float PI= 3.14159f;

    printf("Input the radius of the circle:");
    scanf("%f" , &radius);
    circu = 2.0f * PI * radius;
    area= PI*radius*radius;
    printf("\nThe circonferance is %f" , circu);
    printf("\nThe area is %f" , area);
}

我发现程序运行的问题,但是当我第一次启动它时,它没有显示“输入圆的半径:”有人知道为什么第一个短语没有弹出吗?

seem that works good

 gcc t.c -Wall
t.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    3 | void main()
      |      ^~~~
a@zalman:~/Dokumenty/temp$ ./a.out
Input the radius of the circle:2.0

The circonferance is 12.566360
The area is 12.566360

The Programm seems to work pretty well. If you have Problems with

scanf()

Then try fflush(stdin); Before executing scanf(). It clears stdin and stops a bug, where a scanf() doesnt clear a NEWLINE(\\n) and the next scanf() just reads the NEWLINE so it doesnt have any normal input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM