Printf问题。
我今天想知道一些事情。以下代码:
unsigned char a = 200;
char b = 200;
printf("%d% d",a,b);
给出:
200,-56
怎么会?我没告诉printf第一个参数是未签名的
并且它自己检测到了它。 varargs似乎不太可能。
怎么可能?
谢谢,
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,
praeiji< sp ******* @ gmail.com>写道:
praeiji <sp*******@gmail.com> wrote:
unsigned char a = 200;
char b = 200;
printf("%d%d",a,b);
给出:
200,-56
怎么来的?我没告诉printf第一个参数是未签名的
并且它自己检测到了它。 varargs似乎没有可能。
怎么可能?
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?
两个参数在传递给printf()之前都被提升为int br />
_Ico
-
:wq
^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C
Both arguments get promoted to int before they are passed to printf()
_Ico
--
:wq
^X^Cy^K^X^C^C^C^C
谢谢,
但是他们为什么要对int进行类型转换呢?它是否来自gcc,当它看到
"%d"和printf函数?
Thanks,
But why are they typecasted to int? Does it come from gcc when it sees
"%d" and the printf function?
praeiji写道:
praeiji wrote:
unsigned char a = 200;
char b = 200;
printf("%d%d",a,b);
给出:
200,-56
怎么来的?我没告诉printf第一个参数是未签名的
并且它自己检测到了它。 varargs似乎不太可能。
怎么可能?
谢谢,
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,
转换参数转换默认Pre -
Specifier Type Value Base cision
%d int x(int)x 10 1
这意味着%d格式为int 。如果你给它char和unsigned char它
将它们转换为int。
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.