简体   繁体   中英

First C program in Codeblocks

This is my code, written in Codeblocks with GNU compiler. But everytime, when I execute the program crashes:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main()
{
    const float pi=acos(-1.0);
    double radius=0;
    float kreisflaeche;

    printf("Bitte den Radius eingeben.\n");
    scanf ("%d", radius);
    radius=radius*radius;
    kreisflaeche=pi*radius;
    printf("Mit dem eingegebenen Radius, erhält man %d als Kreisflaeche.", kreisflaeche);

    return 0;
}

"the exe stopped working" but why?

This line scanf ("%d", radius); should be scanf("%lf", &radius);

Radius is not an int so you can't use %d and you need to give the address of the variable where the input has to be stored hence using &

Here is an excerpt from the man page of scanf

The scanf() family of functions scans input according to format as described below. This format may contain conversion specifications; the results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format.

You can refer to this man page for more information on conversion and scanf() in general

change

scanf ("%d", radius);

with

scanf ("%lf", &radius);

EDIT: added arc676's hint

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