comp.lang.c
[Top] [All Lists]

Re: printf and cout

Subject: Re: printf and cout
From: Ben Bacarisse
Date: Fri, 28 Mar 2008 18:00:00 +0000
Newsgroups: comp.lang.c

Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:

> jacob navia said:
>
>> Keith Thompson wrote:
>>> jacob navia <jacob@xxxxxxxxxx> writes:
> <snip>
>>>> 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. Obviously your example
>> will work.
>> 
>> printf("%c",12345678);
>> 
>> will not!
>  
> It is certainly true that it is not guaranteed to work. It is not 
> guaranteed *not* to work, however. On systems with CHAR_BIT >= 24, it 
> /will/ work (although heaven knows what character will be
> printed!). 

Now I am confused!  How could it "not work" on any system?  The int
argument (OK so far) is converted to unsigned char (also OK on all
systems, but the result does depend on CHAR_BIT) and then the resulting
character is printed.  Is it that last bit you worry about?  If so
printf("%c", 32) is not "guaranteed to work" but I did not think that
was your point.

-- 
Ben.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy