John
John

Reputation: 23

C function not returning correct result

I am a beginner of programming, I could not figure out what is wrong with the following code:

#include <stdio.h>
int main(void)
{
long double radius = 0.0L;
long double area = 0.0L;
const long double pi = 3.1415926353890L;
printf("please give the radius ");
scanf("%Lf", &radius);
area = pi * radius * radius;
printf("Area of circle with radius %.3Lf is %.12Lf\n", radius,
area);
return 0;
}

This is actually copied directly from a tutorial, when I ran it, I got 0.000000000000 for area, I tried to change to initialized value of area, but the result did not change, can someone tell me what is wrong here?

Update: I ran it in code::blocks, GNU GCC compiler. I tried 5 as the radius, the radius was printed out correctly, but the area was 0.000.......

Changing from long double to double fixed the issue...

Upvotes: 1

Views: 123

Answers (2)

M.M
M.M

Reputation: 141574

Code::Blocks defaults to an old, buggy compiler. This bug is fixed in mingw-w64 when compiling with the switch -D__USE_MINGW_ANSI_STDIO=1.

You can download and install this compiler into Code::Blocks and then add that option to the compiler options; or put as the first line of your source code:

#define __USE_MINGW_ANSI_STDIO 1

Upvotes: 2

utsav_deep
utsav_deep

Reputation: 631

Your code works perfectly in gcc/g++ compiler but outputs 0.0000.... in case of mingw compiler. This is because mingw uses the Microsoft C library and I seem to remember that that this library has no support for 80bits long double (microsoft C compiler use 64 bits long double for various reasons).

However if you use double instead of long double, then you will always get expected result.

Upvotes: 4

Related Questions