473,587 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print a short instead of an int... ? What's up with "%hd"?


When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can't see any reason why you'd
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);
Martin

Oct 22 '07 #1
17 10787
Martin Wells wrote:
When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can't see any reason why you'd
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);
I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The 'h' adds nothing during the printing
process, but makes a difference in the scanning process.
Oct 22 '07 #2
"James Kuyper Jr." wrote:
>
Martin Wells wrote:
When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can't see any reason why you'd
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);

I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The 'h' adds nothing during the printing
process, but makes a difference in the scanning process.
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Oct 22 '07 #3
Kenneth:
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".

OK I can see how the following are true:

(unsigned)(shor t)-1 == UINT_MAX (maybe 0xffff)
(short unsigned)(short )-1 == USHRT_MAX (maybe 0xffffffff)

....but is it not undefined behaviour to supply printf with a signed
integer type when it expects an unsigned integer type, or vice versa?

If I wanted to print the max value of an unsigned short (without using
USHRT_MAX of course), then I'd go with:

printf("%u",(un signed)(short unsigned)-1)

Martin

Oct 22 '07 #4
On Oct 23, 3:32 am, Kenneth Brody <kenbr...@spamc op.netwrote:
"James Kuyper Jr." wrote:
I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The 'h' adds nothing during the printing
process, but makes a difference in the scanning process.

It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".
Well, you caused undefined behaviour by passing a
signed value. James' comment should be read as saying
it makes no difference to conforming code!

The %hx could in theory allow the compiler to optimize
more, as the printf implementation has the option of
discarding 2 of the 4 bytes read (say) before processing
them.

Oct 23 '07 #5
Old Wolf wrote:
>
On Oct 23, 3:32 am, Kenneth Brody <kenbr...@spamc op.netwrote:
"James Kuyper Jr." wrote:
I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The 'h' adds nothing during the printing
process, but makes a difference in the scanning process.
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".

Well, you caused undefined behaviour by passing a
signed value. James' comment should be read as saying
it makes no difference to conforming code!
You are, of course, correct. I hadn't thought about it before,
but it makes perfect sense that %x takes an unsigned value.
(After all, I would want something like "8000" to display, and
not "-7fff".)
The %hx could in theory allow the compiler to optimize
more, as the printf implementation has the option of
discarding 2 of the 4 bytes read (say) before processing
them.
What does the standard say about a varadic function which gets
passed an int but reads it as short? Given that the standard
requires that (in this instance) a short be promoted to int
anyway, does passing an int invoke UB?

On a system with 16-bit short and 32-bit int, does this invoke
UB?

printf("%hd\n", 65536);

My system displays zero, but "gives you the output you expect"
is allowed under UB. :-)

Section 7.19.6.1p7 seems to allow it:

h Specifies that a following d, i, o, u, x, or X conversion
specifier applies to a short int or unsigned short int
argument (the argument will have been promoted according
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
to the integer promotions, but its value shall be
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^
converted to short int or unsigned short int before
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^
printing); or that a following n conversion specifier
^^^^^^^^
applies to a pointer to a short int argument.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Oct 23 '07 #6
On Oct 24, 3:04 am, Kenneth Brody <kenbr...@spamc op.netwrote:
On a system with 16-bit short and 32-bit int, does this invoke
UB?

printf("%hd\n", 65536);
I would think so, but a more interesting question
is whether this invokes UB:
printf("%hd\n", 0);

We've had this discussion at least once before (in
fact, I initiated it one time). I don't recall the
conclusion however :)

Oct 23 '07 #7
In article <11************ **********@q5g2 000prf.googlegr oups.comOld Wolf <ol*****@inspir e.net.nzwrites:
On Oct 23, 3:32 am, Kenneth Brody <kenbr...@spamc op.netwrote:
....
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".

Well, you caused undefined behaviour by passing a
signed value. James' comment should be read as saying
it makes no difference to conforming code!
Yes, even (unsigned short)2 passed for "%hx" causes undefined behaviour,
when int and short are not essentially the same. So when passing
an "unsigned short", you should cast it to an "unsigned int". Ah, I
like those integer promotion rules. The only arguments types you can
reliably pass to "%x" are unsigned int and longer.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 24 '07 #8
Dik T. Winter wrote:
The only arguments types you can
reliably pass to "%x" are unsigned int and longer.
What is reliable about using a longer type than unsigned,
with "%x"?

--
pete
Oct 24 '07 #9
"Dik T. Winter" <Di********@cwi .nla écrit dans le message de news:
Jq********@cwi. nl...
In article <11************ **********@q5g2 000prf.googlegr oups.comOld Wolf
<ol*****@inspir e.net.nzwrites:
On Oct 23, 3:32 am, Kenneth Brody <kenbr...@spamc op.netwrote:
...
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".
Well, you caused undefined behaviour by passing a
signed value. James' comment should be read as saying
it makes no difference to conforming code!

Yes, even (unsigned short)2 passed for "%hx" causes undefined behaviour,
when int and short are not essentially the same. So when passing
an "unsigned short", you should cast it to an "unsigned int". Ah, I
like those integer promotion rules. The only arguments types you can
reliably pass to "%x" are unsigned int and longer.
It may well be the case under the current wording of the standard, but I
cannot believe it be the intent of the Commitee. It would take an
inordinate amount of bad faith to come up with an implementation that shows
unexpected behaviour for such a case. It would cause enough portability
problems for so many programs to be adequately qualified as *broken*. There
is nothing wrong with passing an int or shorter type for %x format. Passing
anything larger (or longer) is wrong.

--
Chqrlie.
Oct 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
20342
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference...
11
19586
by: Robert Lacoste | last post by:
Dear Access gurus, I was using since years Access 97, now under XP SP2, without any problem. However I've just reformatted my HD (viruses...), reinstalled XP SP2, and then I am no longer able to install Access 97 : When executing the install, I got successively two error messages (sorry, it's probable not exactly the same text than in the...
10
20849
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. ...
1
3495
by: | last post by:
Hi guys! I am a beginner with aspx (i have some background with .asp). And i am trying tu use a 'code-behind' page in order to execute all transactions in that page and leave as clean as possible the .aspx with the html. As my start-point, i am building a login page. I put the textbox and submit button on the page index.aspx; and, i put all...
2
1836
by: Davíð Þórisson | last post by:
hi in my old Asp pages I used Jscript as below that is a single .asp page would contain various subcodes. Is it the same in Asp.net? switch (Request.QueryString("handler").Item) { case "load": ... break case "save":
11
5337
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
23
3182
by: Phil Powell | last post by:
// OBTAINED FROM http://www.javascripter.net/faq/settinga.htm // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY AND THUS EXPIRE function setCookie(name, value, days, docObj) { var today = new Date(); var expire = new Date(); if (days == null || isNaN(days) || days == 0) days = 1; if (days >= 1 || days < 0)...
7
5195
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. http://groups.google.co.in/group/comp.std.c/browse_thread/thread/a656169cb5941cbf/?hl=en# But I want to know what was the conclusion that has been reached. It is unusually long with 146 posts. Therefore it is hard...
1
17000
by: dissectcode | last post by:
Hello - I am looking at a scanf function that takes in a 16 bit unsigned int(UINT16), but its format specifier is %hd. According to http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/standlib/ref/printconversionspecifiers.htm that specifier is for a short int, so won't I lose precision here? ...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.