DIGIT-ARC read-only archive · snapshot: July 23, 2026

Doubt in C

17 posts · originally from https://geek.digit.in/community/threads/doubt-in-c.52560/

abhi_10_20
abhi_10_20 Original post #1
Mar 16, 2007 at 7:48 PM
Dunno whether its right to post it here....

Anyway, this is my doubt:

i couldn't find this in Google.......

---> if we declare a float, like:

float a=2.7;

it gets stored in the memory as 2.700000 with specifically 6 digits only, after the decimal point. Why? (may be related to its format)
nileshgr
Mar 16, 2007 at 8:02 PM
abhi_10_20 said:
Dunno whether its right to post it here....

Anyway, this is my doubt:

i couldn't find this in Google.......

---> if we declare a float, like:

float a=2.7;

it gets stored in the memory as 2.700000 with specifically 6 digits only, after the decimal point. Why? (may be related to its format)
It's predefined in the compiler. This is probably the wrong sect. it shud go in s/w troubleshooting. :)
47shailesh
Mar 16, 2007 at 8:13 PM
Yes by default it is upto 6 point decimal...

But if you want it to 3 point decimal than use %.3f in the print statement to get result in upto 3 place of decimal

e.g.

printf("salary is %.3f\n", salary );

For in depth Knowledge read Dennis Ritche
mediator
Mar 16, 2007 at 8:50 PM
U can set any precision u want! Check my code
#include <iomanip.h>
#include <iostream.h>
int main()
{
int p;
cout<<"Mediator asked to enter precision u wanna set : "; cin>>p;
double x(3.14);
cout <<"x = "<<fixed <<setprecision(p)<<x<<'\n';
cout <<"x/3 = "<<fixed <<setprecision(p)<<(x/3)<<'\n';
return 0;
}
U can set any precision using setprecision! Don't try large numbers or ur terminal might hang! I tried "100000000000" as precision and had to kill the program then!! ;)
abhi_10_20
Mar 16, 2007 at 9:15 PM
thanks for your replies......

but still not satisfied somewhat...
i think it goes with the storage space reserved........like float requires 4 bytes
in a standard turbo c compiler.....and we need to do some 'masala munch' with these 4 bytes.....
nileshgr
Mar 17, 2007 at 11:58 AM
mediator said:
U can set any precision u want! Check my code

U can set any precision using setprecision! Don't try large numbers or ur terminal might hang! I tried "100000000000" as precision and had to kill the program then!! ;)
Buddy, abhi_10_20 is talking about C. Your code is C++! :)
mediator
Mar 17, 2007 at 2:14 PM
^^But his precise problem is about float!
abhi_10_20
Mar 17, 2007 at 3:46 PM
isn't setprecision() found in C ?
mediator
mediator #10
Mar 17, 2007 at 4:50 PM
setprecision is found from library "iomanip.h" and it doesn't matter if its c or c++ afaik!
nileshgr
nileshgr #11
Mar 17, 2007 at 6:38 PM
If you want a detailed manual on programming in C, see snip

i have uploaded it. :)
__________
After giving the captcha code, wait 30 secs. goto the bottom. when the downlaod now button gets activated, click it to download. Don't use any d/w manager. :)
__________
Was it useful? :)
abhi_10_20
Mar 17, 2007 at 9:19 PM
another c book into my kitty........
man...i can start a big library with my pdf's....
nileshgr
nileshgr #13
Mar 17, 2007 at 9:33 PM
abhi_10_20 said:
another c book into my kitty........
man...i can start a big library with my pdf's....
Ha hA ha ha. :lol:
47shailesh
Mar 20, 2007 at 3:55 PM
The Unknown said:
If you want a detailed manual on programming in C, see snip
Same book i was talkin about..

in the pdf mentiontioned page number 22 gives the idea about your problem
abhi1301
abhi1301 #15
Mar 20, 2007 at 4:11 PM
The Unknown said:
Ha hA ha ha. :lol:

Or u better start trusting the replies in here .. Shilesh is perfectly correct .. it's 6 point precision by default and u cannot do anything about it if u want the value just 1 or 2 places do it just like it's written .. change the precision of output wid
printf("salary is %.3f\n", salary );

being more clear this will print upto 3 places after decimal if u write
printf("salary is %.2f\n", salary );

this prints 2 values and so on ..
47shailesh
Mar 20, 2007 at 10:03 PM
The Unknown said:

:shock: this book is "W. Kernighan and Dennis M. Ritchie" is it not a copyright voilation.. if so then remove the link please..
nileshgr
nileshgr #17
Mar 21, 2007 at 2:01 PM
47shailesh said:
:shock: this book is "W. Kernighan and Dennis M. Ritchie" is it not a copyright voilation.. if so then remove the link please..
I don't know. It was given to me by a frnd. :?: