15,936,466 members
1.00/5 (1 vote)
See more:
```#define PI (22/7)
#include<stdio.h>
void main()
{
float x,y;
x=PI;
y=22/7;
printf("\nx=%f y=%f",x,y);
}```

I'm expecting an output as follows x=3.148, y=3.148
but it's as follows
x=3.000000 y=3.000000

What I have tried:

I have used gcc compiler to compile the above code.
Posted
Updated 6-Jun-17 8:47am
v3
ZurdoDev 6-Jun-17 13:23pm
I have not done C in a long, long time but I think you can do either 22.0/7.0 or 22f/7f. They are being treated as integers.
Kornfeld Eliyahu Peter 6-Jun-17 13:43pm
Sorry - I didn't saw your comment (refresh maybe?) but it is truly yours...

## Solution 1

The problem with your numbers (the constants 22 and 7). The compiler interprets them as integers and therefore performs an integer division (it means that the result an integer too)...
So actually 22/7 is compiled to (int)((int)22 / (int)7), that results in 3...
You have free ways to overcome it...
1. Add one place after the decimal point (a ) of course to keep the value) - 22 => 22.0. This will force the compiler to interpret the number as double...
2. Add a suffix too to create a float - 22 => 22.0f...
3. Store values in variables an use them in division -
C++
```float f22 = 22;
float f7 = 7;
float x = f22 / f7;```

CPallini 6-Jun-17 16:49pm
5.
Kornfeld Eliyahu Peter 7-Jun-17 2:14am
Thank you...

## Solution 2

Quote:
What went wrong with this program....?

Simple, this is C behavior.
The C compiler look at 22 and 7, both are integers, so the compiler decide to do an integer division.
To ensure the division will be floating point, the literal must be floating point too.
The compiler knows a literal is a floating point when it encounter a '.'
22 and 7 are integers, 22.0 and 7.0 are same value but floating point.
22/7 => 3
22.0/7.0=> 3.14....