473,545 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Usefulness of %hd in printf

Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type 'short int' ?

Jan 23 '07 #1
34 15850
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type 'short int' ?
Think about the scanf() family of functions.

Sure, the short will promote just like floats promote to double for the
same reason. But addresses do not promote. Hence, the need for a
width specifier for 'smaller than default promotion' types.

Jan 23 '07 #2
user923005 wrote:
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type 'short int' ?

Think about the scanf() family of functions.
I'm asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?

Jan 23 '07 #3
Old Wolf wrote:
user923005 wrote:
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type 'short int' ?
Think about the scanf() family of functions.

I'm asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?
No. I am saying scanf() has to have it, while printf() could care
less.

We could just as easily complain about:

12.9: Someone told me it was wrong to use %lf with printf(). How can
printf() use %f for type double, if scanf() requires %lf?

A: It's true that printf's %f specifier works with both float and
double arguments. Due to the "default argument promotions"
(which apply in variable-length argument lists such as
printf's, whether or not prototypes are in scope), values of
type float are promoted to double, and printf() therefore sees
only doubles. (printf() does accept %Lf, for long double.)
See also questions 12.13 and 15.2.

References: K&R1 Sec. 7.3 pp. 145-47, Sec. 7.4 pp. 147-50; K&R2
Sec. 7.2 pp. 153-44, Sec. 7.4 pp. 157-59; ISO Sec. 7.9.6.1,
Sec. 7.9.6.2; H&S Sec. 15.8 pp. 357-64, Sec. 15.11 pp. 366-78;
CT&P Sec. A.1 pp. 121-33.

Jan 23 '07 #4
>From the C99 standard, here is the bit on default promotions for things
lacking prototypes and for variadic functions...
>From ©ISO/IEC ISO/IEC 9899:1999 (E), 6.5.2.2 Function calls:
"6 If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double. These are called the default argument promotions. If the number
of arguments does not equal the number of parameters, the behavior is
undefined. If the function is defined with a type that includes a
prototype, and either the prototype ends with an ellipsis (, ...) or
the types of the arguments after promotion are not compatible with the
types of the parameters, the behavior is undefined. If the function is
defined with a type that does not include a prototype, and the types of
the arguments after promotion are not compatible with those of the
parameters after promotion, the behavior is undefined, except for the
following cases:
* one promoted type is a signed integer type, the other promoted type
is the corresponding unsigned integer type, and the value is
representable in both types;
* both types are pointers to qualified or unqualified versions of a
character type or void."

Jan 23 '07 #5
user923005 wrote:
Old Wolf wrote:
user923005 wrote:
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type 'short int' ?
>
Think about the scanf() family of functions.
I'm asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?

No. I am saying scanf() has to have it, while printf() could care
less.
So you are saying it does make a difference to printf?
What difference?

Or do you mean "couldn't care less" ?
We could just as easily complain about:

12.9: Someone told me it was wrong to use %lf with printf(). How can
printf() use %f for type double, if scanf() requires %lf?
Huh? I'm not complaining. I'm asking about printf (NOT scanf). You
brought scanf into this.
A: It's true that printf's %f specifier works with both float and
double arguments. Due to the "default argument promotions"
(which apply in variable-length argument lists such as
printf's, whether or not prototypes are in scope), values of
type float are promoted to double, and printf() therefore sees
only doubles. (printf() does accept %Lf, for long double.)
See also questions 12.13 and 15.2.
This is irrelevant to my question. (If you don't know the answer then
just say so, or don't reply)

Jan 23 '07 #6
"Old Wolf" <ol*****@inspir e.net.nzwrote in message
news:11******** **************@ d71g2000cwa.goo glegroups.com.. .
user923005 wrote:
>Old Wolf wrote:
user923005 wrote:
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a
different
result to %d, and the corresponding argument was of type 'short
int' ?

Think about the scanf() family of functions.

I'm asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?

No. I am saying scanf() has to have it, while printf() could care
less.

So you are saying it does make a difference to printf?
What difference?

Or do you mean "couldn't care less" ?
>We could just as easily complain about:

12.9: Someone told me it was wrong to use %lf with printf(). How can
printf() use %f for type double, if scanf() requires %lf?

Huh? I'm not complaining. I'm asking about printf (NOT scanf). You
brought scanf into this.
>A: It's true that printf's %f specifier works with both float and
double arguments. Due to the "default argument promotions"
(which apply in variable-length argument lists such as
printf's, whether or not prototypes are in scope), values of
type float are promoted to double, and printf() therefore sees
only doubles. (printf() does accept %Lf, for long double.)
See also questions 12.13 and 15.2.

This is irrelevant to my question. (If you don't know the answer then
just say so, or don't reply)
%hd prints out the int argument cast to short. So it scrapes off any bits
that can't be represented in a short.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 23 '07 #7
P.J. Plauger wrote:
"Old Wolf" <ol*****@inspir e.net.nzwrote in message
news:11******** **************@ d71g2000cwa.goo glegroups.com.. .
>user923005 wrote:
>>Old Wolf wrote:
user923005 wrote:
Old Wolf wrote:
>Is there any possible situation for printf where %hd causes a
>differen t
>result to %d, and the corresponding argument was of type 'short
>int' ?
Think about the scanf() family of functions.
I'm asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?
No. I am saying scanf() has to have it, while printf() could care
less.
So you are saying it does make a difference to printf?
What difference?

Or do you mean "couldn't care less" ?
>>We could just as easily complain about:

12.9: Someone told me it was wrong to use %lf with printf(). How can
printf() use %f for type double, if scanf() requires %lf?
Huh? I'm not complaining. I'm asking about printf (NOT scanf). You
brought scanf into this.
>>A: It's true that printf's %f specifier works with both float and
double arguments. Due to the "default argument promotions"
(which apply in variable-length argument lists such as
printf's, whether or not prototypes are in scope), values of
type float are promoted to double, and printf() therefore sees
only doubles. (printf() does accept %Lf, for long double.)
See also questions 12.13 and 15.2.
This is irrelevant to my question. (If you don't know the answer then
just say so, or don't reply)

%hd prints out the int argument cast to short. So it scrapes off any bits
that can't be represented in a short.
So the following is a bug?

----------------------------
#include <stdio.h>

int main (void)
{
int a = 1<<30;
short b = a;
printf ("%d %hd %hd\n", a, a, b);
return 0;
}
----------------------------

It prints

1073741824 1073741824 0

i.e. it doesn't truncate 'a' (which of type int) when prints
it with %hd.

Thanks,
Yevgen
Jan 23 '07 #8
Yevgen Muntyan wrote:
So the following is a bug?

----------------------------
#include <stdio.h>

int main (void)
{
int a = 1<<30;
short b = a;
printf ("%d %hd %hd\n", a, a, b);
return 0;
}
----------------------------

It prints

1073741824 1073741824 0

i.e. it doesn't truncate 'a' (which of type int) when prints
it with %hd.
You caused undefined behaviour by not passing an argument of
type 'short int' to %hd

Jan 23 '07 #9
P.J. Plauger wrote:
Old Wolf wrote:
Is there any possible situation for printf where %hd causes a
different result to %d, and the corresponding argument was
of type 'short int' ?

%hd prints out the int argument cast to short. So it scrapes off any bits
that can't be represented in a short.
Well, when the short int is passed as argument, it is promoted to int
(according to the default argument promotions). So how can the int
have any bits that can't be represented in a short?

#include <stdio.h>
int main()
{
short x;
/* your code here */
printf("%hd\n%d \n", x, x);
return 0;
}

Is there any possible code you can insert where marked, that will
cause two different values to be printed (and the program to not
have undefined behaviour) ?

Jan 23 '07 #10

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

Similar topics

11
5920
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
8
2692
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
96263
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me...
188
17233
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
17514
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding (void) to printf. Thanks, Whatluo.
4
1992
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
11
3891
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float...
1
8491
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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
7656
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. ...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5326
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
3450
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...
1
1879
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
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.