Simple scanf/printf question 
Author Message
 Simple scanf/printf question


Quote:


>>For the floating-point input conversions, the default argument type is
>>float* . (This is different from the corresponding output conversions,
where
>>the default type is double ; remember that float arguments to printf are
>>converted to double by the default argument promotions, but float *
>>arguments are not promoted to double * .) You can specify other sizes of
>>float using these type modifiers:

>Doesn't this violate the ANSI standard or am I missing something? I
>have always throught (see also Kernighan & Ritchie) that for printf(),
>you should use %g for floats and %lg for doubles. However, when using
>%lg, GCC gives me the warning: "use of 'l' length character with 'g'
>type character".

  AFAIK floats are converted to doubles when they're passed to an
unprototyped (e.g. std/var args) function anyway, so I couldn't see the
point in using a special indicator when, from printf's point of view,
both types will be passed in as a double. K&R volume 1 says that
the length modifier 'l' "indicates that the corresponding data item is
a long rather than an int", so it would seem not to have been relevant
to floats at all. Of course K&R vol. 1 is a bit old now. I've brought
this thread into comp.lang.c where you can get a definitive answer;
if anyone in c.l.c wants to read the earlier posts in this thread, try


        DaveK
--
They laughed at Galileo.  They laughed at Copernicus.  They laughed at
Columbus. But remember, they also laughed at Bozo the Clown.



Sun, 20 Apr 2003 21:11:58 GMT  
 Simple scanf/printf question
I checked the Standard.  For long doubles, it's %Lg, not %lg.  Use small
l only for long, not long double.

Gregory Pietsch

Quote:




> >>For the floating-point input conversions, the default argument type is
> >>float* . (This is different from the corresponding output conversions,
> where
> >>the default type is double ; remember that float arguments to printf are
> >>converted to double by the default argument promotions, but float *
> >>arguments are not promoted to double * .) You can specify other sizes of
> >>float using these type modifiers:

> >Doesn't this violate the ANSI standard or am I missing something? I
> >have always throught (see also Kernighan & Ritchie) that for printf(),
> >you should use %g for floats and %lg for doubles. However, when using
> >%lg, GCC gives me the warning: "use of 'l' length character with 'g'
> >type character".

>   AFAIK floats are converted to doubles when they're passed to an
> unprototyped (e.g. std/var args) function anyway, so I couldn't see the
> point in using a special indicator when, from printf's point of view,
> both types will be passed in as a double. K&R volume 1 says that
> the length modifier 'l' "indicates that the corresponding data item is
> a long rather than an int", so it would seem not to have been relevant
> to floats at all. Of course K&R vol. 1 is a bit old now. I've brought
> this thread into comp.lang.c where you can get a definitive answer;
> if anyone in c.l.c wants to read the earlier posts in this thread, try


>         DaveK
> --
> They laughed at Galileo.  They laughed at Copernicus.  They laughed at
> Columbus. But remember, they also laughed at Bozo the Clown.



Sun, 20 Apr 2003 23:32:39 GMT  
 Simple scanf/printf question

Quote:



>>>For the floating-point input conversions, the default argument type is
>>>float* . (This is different from the corresponding output conversions,
>where
>>>the default type is double ; remember that float arguments to printf are
>>>converted to double by the default argument promotions, but float *
>>>arguments are not promoted to double * .) You can specify other sizes of
>>>float using these type modifiers:

>>Doesn't this violate the ANSI standard or am I missing something? I
>>have always throught (see also Kernighan & Ritchie) that for printf(),
>>you should use %g for floats and %lg for doubles.

Where *exactly* in Kernighan & Ritchie have you seen "%g for floats and
%lg for doubles" in printf()???

Quote:
>>However, when using
>>%lg, GCC gives me the warning: "use of 'l' length character with 'g'
>>type character".

gcc is right.  In C89, %lg is undefined behaviour in a printf format.
In C99 (which gcc doesn't implement, for the time being) %lg is identical
to %g.  Both expect a double argument, because printf cannot receive
float arguments (it's a variadic function).

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 21 Apr 2003 00:40:36 GMT  
 Simple scanf/printf question

Quote:
>>>Doesn't this violate the ANSI standard or am I missing something? I
>>>have always throught (see also Kernighan & Ritchie) that for printf(),
>>>you should use %g for floats and %lg for doubles.

>Where *exactly* in Kernighan & Ritchie have you seen "%g for floats and
>%lg for doubles" in printf()???

You are right. I looked at the table for scanf(). Printf() doens't
have a %lg type.

Regards,
Joris



Mon, 21 Apr 2003 21:15:35 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

2. tiny question on format of scanf and printf

3. Beginner question - printf and scanf

4. a simple question about printf()

5. skipped printf/scanf ???

6. Examples of format strings for scanf/printf ?

7. scanf/printf conversions

8. pointers in scanf and printf

9. printf() and scanf()

10. casting printf/scanf

11. Q:printf() and scanf()....

12. printf and scanf in IBMC on IBM 3090

 

 
Powered by phpBB® Forum Software