Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Printf question. (http://www.velocityreviews.com/forums/t440704-printf-question.html)

praeiji 12-30-2005 07:21 PM

Printf question.
 
Hi,

I was wondering something today. The following code :

unsigned char a = 200;
char b = 200;
printf( "%d %d", a, b );

gives :

200, -56

How comes? I didn't tell printf that the first argument was unsigned
and it detected it on its own. It doesn't seem possible with varargs.
How is it possible?

Thanks,


usenet@zevv.nl 12-30-2005 07:33 PM

Re: Printf question.
 
praeiji <spacecaps@gmail.com> wrote:
>
> unsigned char a = 200;
> char b = 200;
> printf( "%d %d", a, b );
>
> gives :
>
> 200, -56
>
> How comes? I didn't tell printf that the first argument was unsigned
> and it detected it on its own. It doesn't seem possible with varargs.
> How is it possible?


Both arguments get promoted to int before they are passed to printf()

_Ico

--
:wq
^X^Cy^K^X^C^C^C^C

praeiji 12-30-2005 07:39 PM

Re: Printf question.
 
Thanks,

But why are they typecasted to int? Does it come from gcc when it sees
"%d" and the printf function?


nelu 12-30-2005 07:44 PM

Re: Printf question.
 

praeiji wrote:
> Hi,
>
> I was wondering something today. The following code :
>
> unsigned char a = 200;
> char b = 200;
> printf( "%d %d", a, b );
>
> gives :
>
> 200, -56
>
> How comes? I didn't tell printf that the first argument was unsigned
> and it detected it on its own. It doesn't seem possible with varargs.
> How is it possible?
>
> Thanks,


Conversion Argument Converted Default Pre-
Specifier Type Value Base cision
%d int x (int)x 10 1

That means %d formats int. If you give it char and unsigned char it
will convert them to int.


Jack Klein 12-30-2005 07:49 PM

Re: Printf question.
 
On 30 Dec 2005 11:21:25 -0800, "praeiji" <spacecaps@gmail.com> wrote
in comp.lang.c:

> Hi,
>
> I was wondering something today. The following code :
>
> unsigned char a = 200;
> char b = 200;
> printf( "%d %d", a, b );
>
> gives :
>
> 200, -56
>
> How comes? I didn't tell printf that the first argument was unsigned
> and it detected it on its own. It doesn't seem possible with varargs.
> How is it possible?
>
> Thanks,


This has nothing to do with printf(), per se. It does not know the
difference between the original objects providing the values.

What you are seeing is the result of what the C standard calls the
"default argument promotions". These occur when calling a function
without a prototype in scope, or for the "..." arguments in a variadic
function.

In the case of integer types of lesser rank than int, the "integer
promotions" occur. Since a signed int can hold the value 200, the
value in the unsigned char 'a' is converted to the int value 200.

On your particular system, chars have 8 bits and 'plain' char is
signed. The binary representation of 200 in an 8-bit byte happens to
be the same as the 8-bit 2's complement representation of -56, and
that is the implementation-defined result of assigning the
out-of-range value to a signed 'plain' char.

The integer promotions take the 'plain' char value of -56 and convert
it to an int value of -56.

So the values of 200 and -56 are generated during argument evaluation,
before printf() is called.

On some platforms, char has more than 8 bits, and 200 would remain 200
in a 'plain' char or signed char. On some platforms, 'plain' char is
unsigned, not signed. On platforms with either of these
characteristics, the output would be "200, 200".

And on some platforms, the output would not appear at all because you
did not add a final '\n'.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

praeiji 12-30-2005 08:00 PM

Re: Printf question.
 
thank you :)

Silly me, I'd never heard of the "integer promotions" before.


usenet@zevv.nl 12-30-2005 08:05 PM

Re: Printf question.
 
praeiji <spacecaps@gmail.com> wrote:

> But why are they typecasted to int? Does it come from gcc when it sees
> "%d" and the printf function?


No, they are not typecasted, but `promoted', which is not the same thing.
Promotion is what happens when you call a function that has no prototype, or
when a function with a variable number (variadic) of arguments like printf()
is called. With promotion, all arguments are converted to a generic 'bigger'
type: char and short to int, float to double.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C

Keith Thompson 12-30-2005 08:56 PM

Re: Printf question.
 
Jack Klein <jackklein@spamcop.net> writes:
> On 30 Dec 2005 11:21:25 -0800, "praeiji" <spacecaps@gmail.com> wrote
> in comp.lang.c:

[...]
>> unsigned char a = 200;
>> char b = 200;
>> printf( "%d %d", a, b );
>>
>> gives :
>>
>> 200, -56
>>
>> How comes? I didn't tell printf that the first argument was unsigned
>> and it detected it on its own. It doesn't seem possible with varargs.
>> How is it possible?

[snip]

> In the case of integer types of lesser rank than int, the "integer
> promotions" occur. Since a signed int can hold the value 200, the
> value in the unsigned char 'a' is converted to the int value 200.


Correction: since a signed int can hold any value of type unsigned
char, the value in the unsigned char 'a' is promoted to type int. The
promoted type doesn't depend on the value that happens to be in the
variable; it depends on which type can hold *any* possible value of
the type.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Flash Gordon 12-30-2005 09:34 PM

Re: Printf question.
 
Keith Thompson wrote:
> Jack Klein <jackklein@spamcop.net> writes:
>> On 30 Dec 2005 11:21:25 -0800, "praeiji" <spacecaps@gmail.com> wrote
>> in comp.lang.c:

> [...]
>>> unsigned char a = 200;
>>> char b = 200;
>>> printf( "%d %d", a, b );
>>>
>>> gives :
>>>
>>> 200, -56
>>>
>>> How comes? I didn't tell printf that the first argument was unsigned
>>> and it detected it on its own. It doesn't seem possible with varargs.
>>> How is it possible?

> [snip]
>
>> In the case of integer types of lesser rank than int, the "integer
>> promotions" occur. Since a signed int can hold the value 200, the
>> value in the unsigned char 'a' is converted to the int value 200.

>
> Correction: since a signed int can hold any value of type unsigned
> char, the value in the unsigned char 'a' is promoted to type int.


<annoying pedant>
What, even on a system with CHAR_BIT==16 and sizeof(int)==1 ?
</annoying pedant>

Of course, you are only likely to find such systems in the embedded
world where you may well not have printf.

> The
> promoted type doesn't depend on the value that happens to be in the
> variable; it depends on which type can hold *any* possible value of
> the type.


You are, of course, correct that is is purely a matter of type, not the
value the variable happens to contain.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Charles Richmond 12-31-2005 01:22 AM

Re: Printf question.
 
usenet@zevv.nl wrote:
>
> praeiji <spacecaps@gmail.com> wrote:
>
> > But why are they typecasted to int? Does it come from gcc when it sees
> > "%d" and the printf function?

>
> No, they are not typecasted, but `promoted', which is not the same thing.
> Promotion is what happens when you call a function that has no prototype, or
> when a function with a variable number (variadic) of arguments like printf()
> is called. With promotion, all arguments are converted to a generic 'bigger'
> type: char and short to int, float to double.
>

Type promotion can also happen when you do mixed mode arithmetic.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+


All times are GMT. The time now is 07:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.