scanf/printf conversions 
Author Message
 scanf/printf conversions

Is %u the correct scanf/printf conversion for an unsigned short
integer?
--



Sun, 05 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
Yea
Quote:

> Is %u the correct scanf/printf conversion for an unsigned short
> integer?
> --




Sun, 05 Jan 2003 03:00:00 GMT  
 scanf/printf conversions

Quote:

> Is %u the correct scanf/printf conversion for an unsigned short
> integer?

Strictly, %hu

--

Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
58 K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html (39
to go)



Mon, 06 Jan 2003 03:00:00 GMT  
 scanf/printf conversions


Quote:
>Is %u the correct scanf/printf conversion for an unsigned short
>integer?

It will work for printf, but you definitely want %hu for scanf.

-- Mat.



Mon, 06 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
why %hu ?
and what h is doing here ?

! DAY DREAM IN C !

Quote:



> >Is %u the correct scanf/printf conversion for an unsigned short
> >integer?

> It will work for printf, but you definitely want %hu for scanf.

> -- Mat.



Mon, 06 Jan 2003 03:00:00 GMT  
 scanf/printf conversions

Quote:

>why %hu ?
>and what h is doing here ?

It stands for sHort. They could not use 's' because that is already taken
to mean string.

In a call to scanf, pointers are specified. A pointer to unsigned short
is different from a pointer to short; some way is needed to convey to
scanf which pointer is being passed.

--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.



Mon, 06 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
Berton Allen Earnshaw a crit dans le message ...

Quote:
>Is %u the correct scanf/printf conversion for an unsigned short
>integer?

No, "%u" is for unsigned int. "%h" is for s'h'ort, and "%hu" is for unsigned
short. details here:
http://www.dinkum.com/htm_cl/lib_prin.html
http://www.dinkum.com/htm_cl/lib_scan.html

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FCLC-FAQ: http://www.isty-info.uvsq.fr/~rumeau/FAQ-FCLC/FAQ-FCLC.html
C-tips: http://jackklein.home.att.net



Mon, 06 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
Groovy hepcat CHRIS was jivin' on Wed, 19 Jul 2000 19:06:33 -0500 in
comp.lang.c.
Re: scanf/printf conversions's a cool scene! Dig it!

Quote:
>Yea

>> Is %u the correct scanf/printf conversion for an unsigned short
>> integer?

  Nae! That is the conversion specifier for unsigned int. It should
work for unsigned short in the *printf() functions because arguments
passed to variadic functions are subject to promotion, but not the
*scanf() functions because its arguments are pointers. You must match
the conversion specifier with the correct pointer type in a *scanf()
call.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Thu, 09 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
On Sun, 23 Jul 2000 13:37:43 GMT,


Quote:
>Groovy hepcat CHRIS was jivin' on Wed, 19 Jul 2000 19:06:33 -0500 in
>comp.lang.c.
>Re: scanf/printf conversions's a cool scene! Dig it!

>>Yea

>>> Is %u the correct scanf/printf conversion for an unsigned short
>>> integer?

>  Nae! That is the conversion specifier for unsigned int. It should
>work for unsigned short in the *printf() functions because arguments
>passed to variadic functions are subject to promotion, but not the
>*scanf() functions because its arguments are pointers. You must match
>the conversion specifier with the correct pointer type in a *scanf()
>call.

Actually, on many (most?) implementations using %u for unsigned
short in printf produces undefined behavior.  unsigned short is
promoted to int, not unsigned int, if the range for short is
smaller than that for int.


Thu, 09 Jan 2003 03:00:00 GMT  
 scanf/printf conversions


Quote:
>Actually, on many (most?) implementations using %u for unsigned
>short in printf produces undefined behavior.  unsigned short is
>promoted to int, not unsigned int, if the range for short is
>smaller than that for int.

However, the bit pattern is the same as if it were converted explicitly to
unsigned int. What produces the undefined behaviour?

-- Mat.



Thu, 09 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
On Sun, 23 Jul 2000 17:19:06 +0100, Mathew Hendry

Quote:



>>Actually, on many (most?) implementations using %u for unsigned
>>short in printf produces undefined behavior.  unsigned short is
>>promoted to int, not unsigned int, if the range for short is
>>smaller than that for int.

>However, the bit pattern is the same as if it were converted explicitly to
>unsigned int. What produces the undefined behaviour?

The undefined behavior is produced by the fact that the standard
says that the behavior is undefined (C99 7.19.6.1).


Fri, 10 Jan 2003 03:00:00 GMT  
 scanf/printf conversions


Quote:
>On Sun, 23 Jul 2000 17:19:06 +0100, Mathew Hendry



>>>Actually, on many (most?) implementations using %u for unsigned
>>>short in printf produces undefined behavior.  unsigned short is
>>>promoted to int, not unsigned int, if the range for short is
>>>smaller than that for int.

>>However, the bit pattern is the same as if it were converted explicitly to
>>unsigned int. What produces the undefined behaviour?

>The undefined behavior is produced by the fact that the standard
>says that the behavior is undefined (C99 7.19.6.1).

7.15.1.1 in the n843 draft explicitly allows it. Late change?

-- Mat.



Fri, 10 Jan 2003 03:00:00 GMT  
 scanf/printf conversions

Quote:



>>On Sun, 23 Jul 2000 17:19:06 +0100, Mathew Hendry



>>>>Actually, on many (most?) implementations using %u for unsigned
>>>>short in printf produces undefined behavior.  unsigned short is
>>>>promoted to int, not unsigned int, if the range for short is
>>>>smaller than that for int.

>>>However, the bit pattern is the same as if it were converted explicitly to
>>>unsigned int. What produces the undefined behaviour?

>>The undefined behavior is produced by the fact that the standard
>>says that the behavior is undefined (C99 7.19.6.1).

>7.15.1.1 in the n843 draft explicitly allows it. Late change?

D'oh, sorry, of course printf needn't be implemented using va_arg. :)

-- Mat.



Fri, 10 Jan 2003 03:00:00 GMT  
 scanf/printf conversions
On Mon, 24 Jul 2000 19:12:49 +0100, Mathew Hendry

Quote:



>>On Sun, 23 Jul 2000 17:19:06 +0100, Mathew Hendry



>>>>Actually, on many (most?) implementations using %u for unsigned
>>>>short in printf produces undefined behavior.  unsigned short is
>>>>promoted to int, not unsigned int, if the range for short is
>>>>smaller than that for int.

>>>However, the bit pattern is the same as if it were converted explicitly to
>>>unsigned int. What produces the undefined behaviour?

>>The undefined behavior is produced by the fact that the standard
>>says that the behavior is undefined (C99 7.19.6.1).

>7.15.1.1 in the n843 draft explicitly allows it. Late change?

I'm afraid I don't have the draft.  In the standard 15.1.1
explicitly allows accessing a signed type using its corresponding
unsigned type using the va_arg macro if the value is positive,
but it says nothing about printf.  I find nothing in the standard
requiring printf to be implemented using the va_arg macro.  In
fact, I find nothing in the standard requiring it to be
implemented in C.  What I do find is that for fprintf()
(17.19.6.1)

        The unsigned int argument is converted to ... unsigned
        decimal (u),

        If any argument is not the correct type for the
        corresponding conversion specification, the behavior is
        undefined.

and for printf() (7.19.6.3)

         The printf function is equivalent to fprintf with the
        argument stdout interposed before the arguments to
        printf.

If 15.1.1 said anything about printf() in the draft, then there
must have been a change.

All references to the C99 standard.



Fri, 10 Jan 2003 03:00:00 GMT  
 scanf/printf conversions

Quote:


>>why %hu ?
>>and what h is doing here ?

>It stands for sHort. They could not use 's' because that is already taken
>to mean string.

<OT subtype="etymology">
I've always assumed that the 'h' stood for 'halfword'. Does anyone know
for a fact what the derivation is?
</OT apology="sorry">

              DaveK
--
"Reality is whatever doesn't go away after you stop believing in it."
                                   -- Philip K.{*filter*}



Sun, 12 Jan 2003 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

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

2. Simple scanf/printf question

3. skipped printf/scanf ???

4. tiny question on format of scanf and printf

5. Examples of format strings for scanf/printf ?

6. pointers in scanf and printf

7. Beginner question - printf and scanf

8. printf() and scanf()

9. casting printf/scanf

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

11. printf and scanf in IBMC on IBM 3090

12. printf/scanf trouble on SPARCstation

 

 
Powered by phpBB® Forum Software