TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Linux & Opensource > Unix

printf for "double" and "long double"

Unix

 
 
 
Thread Tools Display Modes
Old 15-09-2007, 05:37 AM   #1
paulwvanc@yahoo.ca
Guest
 
Posts: n/a
printf for "double" and "long double"

hi

how to reliably print for "double" and "long double"?
I've been reading "man 3 printf", but I can't figure it out for
"double" and "long double".
here is my system's sizeof's:
printf("%d %d %d %d\n",sizeof(long
long),sizeof(float),sizeof(double),sizeof(long double));
8 4 8 12
I guessed "%lld" for "long long", and "%f" for "float".
how about "double" and "long double"?
thanks.

 
Old 15-09-2007, 05:37 AM   #2
Keith Thompson
Guest
 
Posts: n/a
Re: printf for "double" and "long double"

paulwvanc@yahoo.ca writes:
> how to reliably print for "double" and "long double"?
> I've been reading "man 3 printf", but I can't figure it out for
> "double" and "long double".
> here is my system's sizeof's:
> printf("%d %d %d %d\n",sizeof(long
> long),sizeof(float),sizeof(double),sizeof(long double));
> 8 4 8 12
> I guessed "%lld" for "long long", and "%f" for "float".
> how about "double" and "long double"?


This is really a C question rather than a Unix question. If you
have more questions, try comp.lang.c.

Use "%f" for both float and double; a float argument to a variadic
function like printf is automatically promoted to double.

Use "%Lf" for long double.

Or replace the 'f' with 'g' or 'e' to control how the number is
formatted; you can also control the precision.

If this isn't explained clearly in your printf man page (it should
be), try any decent C textbook. K&R2 (Kernighan & Ritchie, _The C
Programming Language_, 2nd edition) is excellent, as is H&S5 (Harbison
& Steele, _C: A Reference Manual_).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 

vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional