jacob navia <jacob@xxxxxxxxxx> writes:
Keith Thompson wrote:
jacob navia <jacob@xxxxxxxxxx> writes:
pete wrote:
santosh wrote:
The %c format expects
(and thus treats the corresponding argument as) an unsigned char.
No standard library functions are described
as taking an argument lower ranking than int.
Yes, but the character is promoted to int only for passing it
to printf. The expected argument is a char, not an int
Are you saying there's something wrong, or even unexpected, with this?
#include <stdio.h>
int main(void)
{
int c = '\n';
printf("%c", c);
return 0;
}
The expected argument normally has a value that's representable as an
unsigned char, but its expected type is int.
The expected argument of the printf formatting option!
That's what I am talking about.
Good, because it's also what I'm talking about.
Obviously your example
will work.
Yes, obviously it will. But if I had taken your statement above
literally:
The expected argument is a char, not an int
I'd expect it to fail.
I think I know what you *meant*, but what you actually *wrote* was
less than accurate on two counts. First, the expected argument
is an int (you clearly said it isn't). Second, the argument is
typically going to be within the range of unsigned char, not of
necessarily of plain char (though there's no prohibition on values
outside the range of unsigned char). For example, on a system with
8-bit signed plain char, there's nothing wrong or even unexpected
about ``printf("%c", 255)'' (except that I'd probably use putchar()).
printf("%c",12345678);
will not!
Actually, the behavior is well defined (if and only if 12345678
is within the range of type int). The int value is converted to
unsigned char and the resulting character is written. Assuming
UCHAR_MAX==255, the result is 78; on an ASCII-based system, this
will print 'N'. (I reached that conclusion by reading the code
and consulting the standard; a quick experiment confirmed it.)
You could say it doesn't "work" in the sense that it doesn't print
anything particularly meaningful.
Before you accuse me of playing "word games", consider that words are
the only tool we have here. You wrote that "the expected argument
is a char"; I can't think of any reasonable interpretation by which
that statement is factually correct.