tags:

views:

224

answers:

4

How do I read in a long double in C?

+8  A: 

long double x; scanf("%Lf", &x);

hrnt
+2  A: 

Either hrnt's answer (scanf("%Lf", &x)) or

long double x;
if(read(fd,&x,sizeof(x))!=sizeof(x)) printf("Oops\n");

the question is very vague.

Michael Krelin - hacker
If it's C, and not necessarily on Unix, wouldn't fread be better?
Thomas Padron-McCarthy
I think read() is equally portable. I doubt there's any difference unless you use fscanf on the same file.
Michael Krelin - hacker
hacker: fread is in the C standard. read is not.
Thomas Padron-McCarthy
Thomas, I was talking about de facto portability, not standards. For what I can tell by the question, OP doesn't care much about either.
Michael Krelin - hacker
hacker: Are you saying that read and file descriptors, as shown in your example code above, will work on Windows?
Thomas Padron-McCarthy
Thomas, last time I checked (over 10 years ago, I'm afraid) it worked on Windows.
Michael Krelin - hacker
hacker: Ok, in that case I have no complaint. (But hey, it doesn't work on TOPS-20!)
Thomas Padron-McCarthy
+2  A: 

Be aware that "long double" is a synonym for "double" on Visual Studio.

http://blogs.msdn.com/ericflee/archive/2004/06/10/152852.aspx

RED SOFT ADAIR
A: 

you can use the format specifier %ld for reading long double variables.

Sachin Chourasiya
%ld is long decimal, not long double.
Steve Jessop