printf和cout [英] printf and cout
问题描述
大家好:
这是关于C和C ++的溢出。
int c = 400;
printf ("%c",c);
打印?在屏幕上,''''的ascii是63.
但是
cout<< int(char(400));
它在屏幕上打印-112。
所以,我的问题是为什么来63和-112,
之间有什么关系
他们,
为什么printf和cout的行为如此不同。
两个测试都在VC2005中。
非常感谢。
Hello, everyone:
this is about overflow in C and C++.
int c = 400;
printf("%c", c);
it print ? on screen, and ascii of ''?'' is 63.
but
cout << int(char(400));
it print -112 on screen.
so, my question is why comes 63 and -112, what relations between
them,
why printf and cout behavior so differently.
both tests are in VC2005.
thanks a lot.
推荐答案
int c = 400;
int c = 400;
printf("%c",c);
打印?在屏幕上,''''的ascii是63.
但是
cout<< int(char(400));
它在屏幕上打印-112。
所以,我的问题是为什么来63和-112 ,他们之间有什么关系,
为什么printf和cout行为如此不同。
printf("%c", c);
it print ? on screen, and ascii of ''?'' is 63.
but
cout << int(char(400));
it print -112 on screen.
so, my question is why comes 63 and -112, what relations between them,
why printf and cout behavior so differently.
嗨!
两个值都是相同的,除了''''不是指
字符''?''但是作为一个不可打印的角色。它也看起来不同的是比正常''$'b $ b。
正如Richard Heathfield在comp.lang.c中发布的那样尝试将输出转储到文件中
并使用某种hexdump / hexedit进行检查。
问候
-
Bernhard Schauer
schauer_at_cruxy_dot_net
Hi!
both values are the same, except that the ''?'' is not meant as the
character ''?'' but as an unprintable character. It also "looks" different
than a normal ''?''.
As Richard Heathfield posted in comp.lang.c try to dump the output to a file
and inspect it using some kind of hexdump/hexedit.
regards
--
Bernhard Schauer
schauer_at_cruxy_dot_net
3月26日凌晨2:05 * am,Bernhard Schauer< bernhard.scha ... @ gmail.com>
写道:
On Mar 26, 2:05*am, Bernhard Schauer <bernhard.scha...@gmail.com>
wrote:
int c = 400;
printf("%c", C);
int c = 400;
printf("%c", c);
打印?在屏幕上,'''''的ascii是63.
it print ? on screen, and ascii of ''?'' is 63.
但
cout<< INT(炭(400));
but
cout << int(char(400));
它在屏幕上打印-112。
it print -112 on screen.
所以,我的问题是为什么来63和-112,他们之间有什么关系,
为什么printf和cout行为如此不同。
so, my question is why comes 63 and -112, what relations between them,
why printf and cout behavior so differently.
嗨!
这两个值是相同的,除了''''不是指
字符''?''但是作为一个不可打印的角色。它也看起来不同的是比正常''$'b $ b。
正如Richard Heathfield在comp.lang.c中发布的那样尝试将输出转储到文件中
并使用某种hexdump / hexedit进行检查。
问候
-
Bernhard Schauer
schauer_at_cruxy_dot_net
Hi!
both values are the same, except that the ''?'' is not meant as the
character ''?'' but as an unprintable character. It also "looks" different
than a normal ''?''.
As Richard Heathfield posted in comp.lang.c try to dump the output to a file
and inspect it using some kind of hexdump/hexedit.
regards
--
Bernhard Schauer
schauer_at_cruxy_dot_net
谢谢。
我只知道:
就像你说的那样,''''并不意味着溢出的结果,
但是ascii字符未知的符号。当一个数字溢出
char,而左边的二进制文件为否定时,printf将打印''?''。
thanks.
I just know it:
Just as you say, ''?'' does not mean to be the result of the overflow,
but a symbol of unknown ascii character. when a number overflows by
char, and the left binary is negate, the printf will print ''?''.
laikon< la****@gmail.comwrites:
laikon <la****@gmail.comwrites:
大家好:
这是关于C和C ++中的溢出。
int c = 400;
printf("%c",c);
it打印?在屏幕上,'''''的ascii是63.
Hello, everyone:
this is about overflow in C and C++.
int c = 400;
printf("%c", c);
it print ? on screen, and ascii of ''?'' is 63.
这取决于区域设置,
$ b使用什么字符编码$ b你的终端。
通常C char是一个八位字节。所以当你想把400当作char时,
得到400%256,即144.但是144不是ASCII代码,并且
既不是代码一个ISO-8859-1字符(因此既不是Unicode字符的
代码)。它是一个控制代码:DCS,Device
控制字符串。您的终端不会将此控制代码解释为
,并以问号的形式打印其困惑。你会给b $ b做同样的事。
That would depend on the locale, on what character encoding is used by
your terminal.
Usually C char are one octet. So when you want to treat 400 as a char,
you get 400 % 256, which is 144. But 144 is not an ASCII code, and
neither the code of an ISO-8859-1 character (and therefore neither the
code of an Unicode character). It''s a control code : DCS, Device
Control String. Your terminal doesn''t interpret this control code, as
such, and prints its perplexity in the form of a question mark. You''d
do the same.
但
cout<< int(char(400));
它在屏幕上打印-112。
but
cout << int(char(400));
it print -112 on screen.
看起来你的字符是签名字符。
400%256 == 144%256 == -112%256
Looks like your chars are signed chars.
400 % 256 == 144 % 256 == -112 % 256
所以,我的问题是为什么来63和-112,
之间有什么关系,
为什么printf和cout行为如此不同。
so, my question is why comes 63 and -112, what relations between
them,
why printf and cout behavior so differently.
嗯,他们不一样,一个是C函数,另一个是C ++
对象。但关键是他们没有机会做同样的事情
无论如何。你给整数400打印,然后给cout给出整数
-112。尝试:
:: printf("%d \ n",int(char(400)));
cout<< int( char(400))<< endl;
然后:
:: printf("%c\ n" ,char(400));
cout<< char(400)<< endl;
当然,你会得到不同的结果因为
cout处理字符的方式,即整数,但这是另一个
的故事。你可以尝试:
char str [2]; str [0] = char(400); str [1] = 0;
:: printf ("%s \ nn",cstr);
cout<< cstr<< endl;
Well they''re not the same, one is a C function, the other is a C++
object. But the point is that they don''t get a chance to do the same
anyways. You give the integer 400 to print, and you give the integer
-112 to cout. Try:
::printf("%d\n",int(char(400)));
cout<<int(char(400))<<endl;
and then:
::printf("%c\n",char(400));
cout<<char(400)<<endl;
and there of course, you get different results because of the way
cout handles characters, that is, as integers, but that''s another
story. You could try:
char str[2];str[0]=char(400);str[1]=0;
::printf("%s\n",cstr);
cout<<cstr<<endl;
两个测试都在VC2005中。
非常感谢。
both tests are in VC2005.
thanks a lot.
-
__Pascal Bourguignon__
--
__Pascal Bourguignon__
这篇关于printf和cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!