printf("%hhd",89);

R

Rajesh S R

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 to follow the
whole discussion.

Therefore I request the authors and those who followed the discussion
to summarize the conclusion that was reached so that it can be of some
help to the programmers.

I apologise for the inconvenience.

Thanks in advance for the reply.
 
O

osmium

Rajesh S R said:
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 to follow the
whole discussion.

Therefore I request the authors and those who followed the discussion
to summarize the conclusion that was reached so that it can be of some
help to the programmers.

See if you can tease the answer out of this Dinkumware link while you are
waiting for another answer.

http://www.dinkum.com/manuals/?manual=compleat&page=lib_prin.html#Print Functions
 
K

Keith Thompson

Rajesh S R said:
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#

Readers in the US should probably use "groups.google.com" rather than
"groups.google.co.in".
But I want to know what was the conclusion that has been reached. It
is unusually long with 146 posts. Therefore it is hard to follow the
whole discussion.

Therefore I request the authors and those who followed the discussion
to summarize the conclusion that was reached so that it can be of some
help to the programmers.

(I started that thread.)

I'm not sure that any firm conclusion was reached. As a practical
matter, it's going to work in any reasonable implementation. The
problem, I think, is that the standard says printf and friends are
variadic functions, but it doesn't actually say that they work the
same way as a function written in C using <stdarg.h>. It's reasonable
to assume that they do, but the standard doesn't explicitly say so.

As you say, it was a very long thread, and since it was several months
ago I don't remember everything that was discussed. The only real way
to determine whether a consensus was reached would be to read the
thread itself.

Also, note that the thread was in comp.std.c, not comp.lang.c, so I'm
not sure why you posted here. On the other hand, posting their most
likely either (a) wouldn't give you a definitive answer, or (b) would
spawn a lengthy thread rehashing the same arguments, possibly both.

The standard's wording is not 100% precise. The question, I suppose,
is how precise it really needs to be. It's been argued that the
wording of the standard has to be interpreted on the basis of common
sense; the problem is that not everybody's common sense is the same.
 
P

P.J. Plauger

The debate was about a subtlety of the wording of the standard itself.
In a quick look over the dinkum.com page, I don't see anything that's
relevant to the question.

Dunno what the question is, but this excerpt *should* be relevant:

Conversion Argument Converted Default Pre-
Specifier Type Value Base cision
......
%hhd int x (signed char)x 10 1 added
with C99

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
K

Keith Thompson

P.J. Plauger said:
Dunno what the question is, but this excerpt *should* be relevant:

Conversion Argument Converted Default Pre-
Specifier Type Value Base cision
.....
%hhd int x (signed char)x 10 1 added
with C99

The issue is that the standard imposes certain specific requirements
on the arguments to printf(); it's not clear whether it has to treat
its arguments as if it were a user-written variadic function using
<stdarg.h>. For example, printf with a "%hd" format expects an
argument of type short, and "If any argument is not the correct type
for the corresponding conversion specification, the behavior is
undefined." Taken literally, that would imply that passing an int
argument, as in printf("%hd", 42) would invoke UB -- but if printf
acts like an ordinary variadic function, then printf("hd", 42) is
exactly equivalent to printf("%hd", (short)42).
 
P

P.J. Plauger

The issue is that the standard imposes certain specific requirements
on the arguments to printf(); it's not clear whether it has to treat
its arguments as if it were a user-written variadic function using
<stdarg.h>. For example, printf with a "%hd" format expects an
argument of type short, and "If any argument is not the correct type
for the corresponding conversion specification, the behavior is
undefined." Taken literally, that would imply that passing an int
argument, as in printf("%hd", 42) would invoke UB -- but if printf
acts like an ordinary variadic function, then printf("hd", 42) is
exactly equivalent to printf("%hd", (short)42).

Okay, now I understand the quibble. So the table entry above *is*
relevant, at least to the extent that it takes a position on the
matter. (Note Argument Type entry.) I will not further comment
on the pinhead area taken up by one dancing angel.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top