Printf问题。 [英] Printf question.

查看:71
本文介绍了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.


这篇关于Printf问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆