Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf() and scanf() questions

Reply
Thread Tools

printf() and scanf() questions

 
 
Micah Cowan
Guest
Posts: n/a
 
      03-05-2008
Ioannis Vranos <(E-Mail Removed)> writes:

> Micah Cowan wrote:
>>>> scanf() questions:
>>>>
>>>> 1. scanf() provides the "%f" specifier for float. How can it
>>>> discern when we pass a double?
>>> %f indicates floating point in general, not the type 'float'.
>>> If you are supplying a pointer to a float, you use "%hf".
>>> If you are supplying a pointer to a double, you use "%f".
>>> If you are supplying a pointer to a long double, you use "%Lf".

>>
>> Er, no. %f takes a float, %lf takes a double, and %Lf takes a long
>> double. The 'h' length modifier is used to specify (signed or
>> unsigned) short int only.

>
> You got my scanf() question answered: I did not remember that there
> was a separate specifier for double, the "%lf", I thought "%f" was for
> both.


To improve consistency, C99 has added "%lf" to printf() as equivalent
to "%f"; however, using it in pre-C99 code gives undefined behavior,
so it's best avoided on code meant for pre-C99 implementations.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      03-05-2008
Ioannis Vranos <(E-Mail Removed)> writes:

>> The answer is that for functions that do not have a prototype, and
>> the non-prototyped positions of varadic function calls, the
>> values passed *always* undergo "the usual argument promotions".

>
>
> I am not sure what you mean by this. #include <stdio.h> is assumed in
> my printf() code and I am not using the implicit C function
> declaration stuff.


Right. He's just saying that what he's saying applies to
implicitly-defined functions in addition to the rest.

> From this sentence of yours: "non-prototyped positions of variadic
> function calls," I assume you mean the "..." stuff.


Yup.

> Here is why I
> wonder. Inside the definition of a variadic function we define the
> "type step" of va_arg, based on an assumed input type, so if we expect
> double we do:
>
> [va_list curr_item;
>
> double curr_val;
> ... ]
>
>
> curr_val= va_arg(curr_item, double);
>
>
> so if we pass a float and %f is about double, we will use the above
> expression, with the wrong "step type".


Nope. He explains why here:

>> Those promote any passed (single precision) float to double,
>> -before- the receiving function sees it. The compiler knows to do
>> this promotion because it knows what the type of the actual argument
>> is, and it knows that it is handing it to a varadic or non-prototyped
>> function.


float is always promoted to double when passing it as an argument
through "...". Note that this implies that

curr_val= va_arg(curr_item, float);

Is never correct, no matter what you pass in.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-06-2008
Walter Roberson wrote:

>> scanf() questions:
>>
>> 1. scanf() provides the "%f" specifier for float. How can it discern
>> when we pass a double?

>
> %f indicates floating point in general, not the type 'float'.


Wrong. "%f", "%a", "%e", and "%g" are for floats.

> If you are supplying a pointer to a float, you use "%hf".


Wrong. "%f", "%a", "%e", and "%g" are for floats;
the 'h' prefix has meaning only in "%hd", "%hi", "%hu", "%ho", "%hx",
and "%hn". "%hf" is incoherent nonsense.

> If you are supplying a pointer to a double, you use "%f".


Wrong. "%lf", "%la", "%le", and "%lg" are for doubles. See above.

> If you are supplying a pointer to a long double, you use "%Lf".


At least you got one right. Have you ever considered knowing what
you're talking about before giving answers?

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-06-2008
Martin Ambuhl wrote:
> Walter Roberson wrote:
>
>>> scanf() questions:
>>>
>>> 1. scanf() provides the "%f" specifier for float. How can it discern
>>> when we pass a double?

>>
>> %f indicates floating point in general, not the type 'float'.


> Wrong. "%f", "%a", "%e", and "%g" are for floats.


Are you sure?

7.19.6.2(12)
"a,e,f,g Matches an optionally signed floating-point number,
whose format is
the same as expected for the subject sequence of the strtod function.
The corresponding argument shall be a pointer to floating."

Note that strtod converts its argument to a double.

Myself, I suspect this part of the Standard needs reworded.

>> If you are supplying a pointer to a float, you use "%hf".

>
> Wrong.


Correct.

> "%f", "%a", "%e", and "%g" are for floats;


For the wrong reason...

>> If you are supplying a pointer to a double, you use "%f".

>
> Wrong.


Incorrect.

>"%lf", "%la", "%le", and "%lg" are for doubles.


though this is correct

> Have you ever considered knowing what you're talking about before giving answers?


You might want to review that remark.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-06-2008
Mark McIntyre wrote:
>
>>> If you are supplying a pointer to a double, you use "%f".

>>
>> Wrong.

>
> Incorrect.


AFAIK "%f" format sepcifier for fscanf()/scanf()is for float *. K&R2
says so. It also says that "%lf" is for double and "%Lf" for long double.
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-06-2008
Ioannis Vranos wrote:
> Mark McIntyre wrote:
>>
>>>> If you are supplying a pointer to a double, you use "%f".
>>>
>>> Wrong.

>>
>> Incorrect.

>
> AFAIK "%f" format sepcifier for fscanf()/scanf()is for float *. K&R2
> says so. It also says that "%lf" is for double and "%Lf" for long double.



I remind you, I am talking about C95 here.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-06-2008
Mark McIntyre wrote:
> Martin Ambuhl wrote:
>> Walter Roberson wrote:
>>
>>>> scanf() questions:
>>>>
>>>> 1. scanf() provides the "%f" specifier for float. How can it discern
>>>> when we pass a double?
>>>
>>> %f indicates floating point in general, not the type 'float'.

>
> > Wrong. "%f", "%a", "%e", and "%g" are for floats.

>
> Are you sure?


Yes.
>
> 7.19.6.2(12)
> "a,e,f,g Matches an optionally signed floating-point number,
> whose format is
> the same as expected for the subject sequence of the strtod function.
> The corresponding argument shall be a pointer to floating."
>
> Note that strtod converts its argument to a double.
>
> Myself, I suspect this part of the Standard needs reworded.


Yourself, you need to read. "a,e,f,g" in the above refers to the
conversion operation and are not full specifiers. "%f" is a specifier,
in which the optional size specification is empty. It is not the same
thing at all.

I have mercifully snipped the remainder of your nonsense. Except:


>> Have you ever considered knowing what you're talking about before
>> giving answers?

>
> You might want to review that remark.


I have. I know what I'm talking about. You haven't a clue.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-06-2008
Ioannis Vranos wrote:
> Mark McIntyre wrote:
>>
>>>> If you are supplying a pointer to a double, you use "%f".
>>>
>>> Wrong.

>>
>> Incorrect.

>
> AFAIK "%f" format sepcifier for fscanf()/scanf()is for float *. K&R2
> says so. It also says that "%lf" is for double and "%Lf" for long double.


Pay no attention to Mark McIntyre. He doesn't have a clue.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      03-06-2008
Mark McIntyre <(E-Mail Removed)> writes:

> Martin Ambuhl wrote:
>> Walter Roberson wrote:
>>
>>>> scanf() questions:
>>>>
>>>> 1. scanf() provides the "%f" specifier for float. How can it
>>>> discern when we pass a double?
>>>
>>> %f indicates floating point in general, not the type 'float'.

>
>> Wrong. "%f", "%a", "%e", and "%g" are for floats.

>
> Are you sure?
>
> 7.19.6.2(12)
> "a,e,f,g Matches an optionally signed floating-point number,
> whose format is the same as expected for the subject sequence of the
> strtod function. The corresponding argument shall be a pointer to
> floating."


You are talking nonsense again.

These are conversion operations and not conversion specifications. You
will notice the absence of the %. Perhaps you should take your own
advice and RTFM?


A word of advice : taking the CLC "standard" of being an obnoxious know
all can leave you looking very stupid indeed.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-06-2008
Ioannis Vranos wrote:
>
> The questions are about C95.
>
> printf() questions:
>
> 1. printf() provides "%f" format specifier for double. Usually
> implemented as a variadic function how can it discern if the argument is
> float or double? Shouldn't we cast to double when passing a float value?
> If an implicit conversion takes place, at what stage does it take place?


No. printf doesn't specify variable types for the variadic
params. So the standard promotions for arguments take place. That
means all chars and shorts will be passed as ints, all floats as
doubles, etc.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments