为什么printf(“%c \ n”,b)无法得到想要的结果? [英] why printf("%c\n", b) can not get the wanted result?

查看:81
本文介绍了为什么printf(“%c \ n”,b)无法得到想要的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

printf("%c",b)无法正常工作。

#include< stdio.h>

int a,b;

char d,e;

char * p;

float f;


int main(int argc,char * argv [])

{

unsigned char a = 0;

printf(a:%d,b:%d,d:%c,e:%c,p:%p,f:%f \ n,a,b,d,e,p,f );

返回0;

}


编程运行结果如下

a: 0,b:0,d:,e :, p :( nil),f:0.000000


如果我在windows xp中运行它,我得到了

a:0,b:0,d:a,e:a,p:(nil),f:0.000000

为什么它不能printf char类型变量d& e?


如果我将printf语句更改为


printf(" a:%d,b:%d,d: %x,e:%x,p:%p,f:%f \ n",a,b,d,e,p,f);

a:0,b:0, d:0,e:0,p:(nil),f:0.000000


i得到了我想要的东西。


但我会问为什么?我对我的问题的回答是因为d,e是值为0的char $ / $
变量,也许%c只有print char,它们是可读的字符

,例如''?'' ,''a-z''等但是值0不是可读字符,所以在

linux下,printf(%c,0)将不会打印任何内容。


如果我的回答是正确的,为什么在windowz下,它打印出来?

hi all,
printf("%c",b) doesn''t work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p , f);
return 0;
}

the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000
why it can not printf char type variable d & e?

and if i change the printf statement to

printf("a:%d,b:%d,d:%x,e:%x,p:%p,f:%f\n",a,b,d,e,p , f);
a:0,b:0,d:0,e:0,p:(nil),f:0.000000

i got what i want.

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as ''?'',''a-z'' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.

if my answer is right, why under windowz , it prints a?

推荐答案



" baumann @ pan" < BA ********* @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
大家好,
printf("%c",b)无法正常工作。
#include< stdio.h>
int a,b;
char d,e;
char * p;
float f;

int main(int argc,char * argv [])
{
unsigned char a = 0;
printf(" a:%d,b:%d,d:%c,e:%c,p:%p,f:%f \ n", a,b,d,e,p,f);


你没有初始化你的变量除了 - 你必须先给它们

值才能打印出来!

return 0;
}

编程结果如下:a:0,b:0,d:,e:,p:(nil),f:0.000000

如果我在windows xp中运行它,我得到了
a:0,b:0,d:a,e:a,p:(nil),f:0.000000

为什么它不能printf char类型变量d& è?


您需要为它们分配一个值,例如char d =''A'';

如果我将printf语句更改为

printf(" a:%d,b:%d,d:%x,e:%x,p:%p,f:%f \ n",a,b,d,e ,p,f);
a:0,b:0,d:0,e:0,p :( nil),f:0.000000

我得到了我想要的东西。

但我会问为什么?我对我的问题的回答是因为d,e是值为0的char变量,也许%c只有print char,它们是可读的字符,例如''''',''a-z''等等,但值0不是可读的字符,因此在
linux下,printf(%c,0)将不会打印任何内容。


0被定义为c中的空字符 - 虽然通常写为''\ 0''

因此它不会打印出来。
hi all,
printf("%c",b) doesn''t work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p , f);
You havent initialised your variables except for a - you must give them
values before you can print them out!
return 0;
}

the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000
why it can not printf char type variable d & e?
you need to assign a value to them, e.g char d = ''A'';

and if i change the printf statement to

printf("a:%d,b:%d,d:%x,e:%x,p:%p,f:%f\n",a,b,d,e,p , f);
a:0,b:0,d:0,e:0,p:(nil),f:0.000000

i got what i want.

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as ''?'',''a-z'' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.
0 is defined as the null character in c - although usually written as ''\0''
therefore it is not printed out.

if my answer is right, why under windowz , it prints a?



,因为你已经进入了未定义的bahaviour领域。因为

变量没有初始化,所以编译器可以做任何事情。

想要的。

Allan



because you have entered the realm of undefined bahaviour. Because the
variables are not initialised the compiler is allowed to do anything it
wants.
Allan


因为这些变量是全局的,所以从结果中可以看出它们的值为0.




来源代码应该是

#include< stdio.h>

int a,b;

char d,e;

char * p;

浮动f;

int main(int argc,char * argv [])

{

// unsigned char a = 0; //它是没用的

printf(" a:%d,b:%d,d:%c,e:%c,p:*%p,f:%f \\ \\ n,a,b,d,e,p,f);

返回0;


}

since these variables are global, so they got initilized with value 0.
as you can see from the result.

the source code should be
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;
int main(int argc, char* argv[])
{
// unsigned char a = 0; // it''s useless
printf("a:%d,b:%d,d:%c,e:%c,p:*%p,f:%f\n",a,b,d,e, p, f);
return 0;

}





Allan Bruce写道:


Allan Bruce wrote:
" baumann @ pan" < BA ********* @ gmail.com>在消息中写道
新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ......
"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
大家好,
printf("%c",b)无法正常工作。
#include< stdio.h>
int a,b;
char d,e;
char * p;
float f;

int main(int argc,char * argv [])
{
unsigned char a = 0;
printf(" a:%d,b:%d,d:%c,e:%c,p:%p,f:%f \ n", a,b,d,e,p,f);
hi all,
printf("%c",b) doesn''t work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p , f);



你没有初始化变量,除了a - 你必须先给它们
值才能打印出来!


You havent initialised your variables except for a - you must give them
values before you can print them out!




再看一遍:所有变量都已初始化,

最多为某种形式的零和'p''为NULL。



Look again: All the variables have been initialized,
most to some form of zero and `p'' to NULL.

编程运行结果如下:a:0,b:0,d:,e:,p:(nil),f :0.000000

如果我在windows xp中运行它,我得到了
a:0,b:0,d:a,e:a,p:(nil),f:0.000000

[...]

但是我有罪问为什么?我对我的问题的回答是因为d,e是值为0的char变量,也许%c只有print char,它们是可读的字符,例如''''',''a-z''等等,但值0不是可读的字符,因此在
linux下,printf(%c,0)将不打印任何内容。
the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000

[...]

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as ''?'',''a-z'' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.



0被定义为c中的空字符 - 虽然通常写为''\ 0''
因此它不打印出来。


0 is defined as the null character in c - although usually written as ''\0''
therefore it is not printed out.




它没有打印出来 ;不是标准

所要求的行为。标准规定了文本流(如

stdout)必须如何处理可打印字符和一些控制

字符,但没有描述会发生什么事情
到其他角色。这允许实现将他们自己的解释附加到输出,例如\ 033 [2J]。 -

当然,一个依赖于这种特殊解释的程序

是不便携的,但便携性不是唯一重要的

特征一个程序。


特别是,标准没有描述将''\ 0''字符发送到文本流的效果

;实施

可以随意做任何事情。看来,OP使用的两个

系统对待''\ 0''的方式不同:一个打印

它作为非图形,非间距字符,而另一个

打印它类似于小写的''a''(那是'

它是如何出现在我的新闻阅读器中的,但我很自信它

看起来在OP的屏幕上略有不同。


-
Er ********* @ sun.com



"It is not printed out" is not a behavior the Standard
requires. The Standard specifies how a text stream (like
stdout) must treat printable characters and a few control
characters, but doesn''t describe what happens what happens
to other characters. This allows implementations to attach
their own interpretations to outputs like "\033[2J" -- of
course, a program that relies on such special interpretation
is non-portable, but portability is not the only important
characteristic of a program.

In particular, the Standard does not describe the effect
of sending a ''\0'' character to a text stream; the implementation
is free to do whatever it likes. It appears that the two
systems the O.P. uses treat a ''\0'' differently: one "prints"
it as a non-graphic, non-spacing character, while the other
"prints" it as something resembling a lower-case ''a'' (that''s
how it appears in my newsreader, but I''m fairly confident it
looks a little different on the O.P.''s screen).

--
Er*********@sun.com


这篇关于为什么printf(“%c \ n”,b)无法得到想要的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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