0
Why pi value is not displayed accurately?
https://code.sololearn.com/cvkXqi2Xqdn4/?ref=app Why my output is just 3.000000 when it should be 3.14...
8 Réponses
+ 3
Verstappen
Because operation happens from right to left. So first it will be divide then assign to double variable.
You can also do this
double a=(double)22/7;
+ 2
Because you are dividing two integers
(22 and 7)
So, the result will be an integer.
try 22.0/7 or 22/7.0 or 22.0/7.0
+ 2
in the C language if you divide
numbers the type of the result will be the type with max memory,
so:
int/int = int
double/int =double
....
so in your case,
double a=22/7;
you just convert an int into a double.
+ 1
zak aria thanks! But why does this happen? I declared the variable as double , so isn't 22/7 supposed to be taken as a double value ?
+ 1
I ᴀᴍ "Tɪᴍᴇ" Thanks now I get it
+ 1
zak aria ohh, but what it i divide
float/int,
as they both have same size,
will the answer be assigned as int or float?
+ 1
it will be double, why...? I don't know hhhh
+ 1
zak aria, Verstappen implicit conversion isn't only about size in memory, you can see the documentation here:
https://en.cppreference.com/w/c/language/conversion