与printf相关的问题 [英] Question related to printf

查看:76
本文介绍了与printf相关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个关于printf功能行为的问题。

表7中K& R2的页数154 1声明


字符参数类型:打印为


d,i int,十进制数

c int ;单个字符


现在假设以下代码


#include< stdio.h>


int main(无效)

{

printf("%c",''1''+ 1);

返回0 ;

}


程序的输出为2.


因为整数值为''1 ''是49所以49 + 1 = 50,字符

相当于十进制50是2.

但我怀疑是

类型表达式1+ 1是整数。因此,如果我使用%c来打开
整数将显示未定义的行为吗?

从K& R2描述中我认为它不会显示未定义的行为。

我的理解是否正确?

问候,

Somenath

Hi All,

I have one question regarding the behavior of printf function.
In page number of 154 in K&R2 in Table 7-1 it is stated that

Characters Argument Type : Printed As

d,i int, decimal number
c int; single character

Now suppose for the following code

#include<stdio.h>

int main(void)
{
printf("%c", ''1''+ 1);
return 0;
}

The output of the program is 2 .

Because the integer value of ''1'' is 49 so 49+1 =50 ,character
equivalent of decimal 50 is 2 .
But my doubt is
The type of the expression ''1'' + 1 is integer. So if I use %c to
print the integer will it show undefined behavior?
From the K&R2 description i think it will not show undefined behavior.
Is my understanding correct?
Regards,
Somenath

推荐答案

somenath< so ********* @ gmail.comwrote:
somenath <so*********@gmail.comwrote:

表7中K& R2中的154页数-1表示


字符参数类型:打印为


d,i int,十进制数

c INT;单个字符


现在假设以下代码


#include< stdio.h>


int main(无效)

{

printf("%c",''1''+ 1);

返回0 ;

}


程序的输出为2.


因为整数值为''1 ''是49所以49 + 1 = 50,字符

相当于小数50是2。
In page number of 154 in K&R2 in Table 7-1 it is stated that

Characters Argument Type : Printed As

d,i int, decimal number
c int; single character

Now suppose for the following code

#include<stdio.h>

int main(void)
{
printf("%c", ''1''+ 1);
return 0;
}

The output of the program is 2 .

Because the integer value of ''1'' is 49 so 49+1 =50 ,character
equivalent of decimal 50 is 2 .



差不多。 1和2的绝对值49和50不保证

(因为标准不需要ASCII),但ISO C _does_ demand

紧跟''1'后'2''。

Almost. The absolute values 49 and 50 for ''1'' and ''2'' are not guaranteed
(because the Standard doesn''t require ASCII), but ISO C _does_ demand
that ''2'' immediately follows ''1''.


但我怀疑是
But my doubt is



问题。你的_question_是。

Question. Your _question_ is.


表达式1+ 1的类型是整数。所以,如果我使用%c来打印整数,它会显示未定义的行为吗?
The type of the expression ''1'' + 1 is integer. So if I use %c to
print the integer will it show undefined behavior?



从ISO C标准(1989年,碰巧,但C99是相同的):


#4.9。 6.1 fprintf函数

....

#c将int参数转换为unsigned char,并且

#结果字符为
注意:_int_参数,转换为unsigned char。所以没有,因为你认为它没有未定义的行为。


Richard

From the ISO C Standard (1989, as it happens, but C99 is the same):

# 4.9.6.1 The fprintf function
....
# c The int argument is converted to an unsigned char, and the
# resulting character is written.

Note: _int_ argument, which is converted to an unsigned char. So no, as
you thought, it does not have undefined behaviour.

Richard


Richard Bos写道:
Richard Bos wrote:

somenath< so ********* @ gmail.comwrote:
somenath <so*********@gmail.comwrote:

>但我怀疑是
>But my doubt is



问题。你的_question_是。


Question. Your _question_ is.



请不要让'重新开启此讨论。就印度语英语


每次印度人发布一个

怀疑/问题时,真的不值得争辩......

Please don''t let''s reopen this discussion. As far as Indian English
speakers are concerned, this use of the term "doubt" is perfectly valid.

It''s really not worth arguing this out each time an Indian posts a
"doubt"/question...


12月12日晚上9点04分,r ... @ hoekstra-uitgeverij.nl(Richard Bos)写道:
On Dec 12, 9:04 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

somenath< somenath ... @ gmail.comwrote:
somenath <somenath...@gmail.comwrote:

表7-1中K& R2中的154页数表示那个
In page number of 154 in K&R2 in Table 7-1 it is stated that


字符参数类型:打印为
Characters Argument Type : Printed As


d,i int ,十进制数

c int;单个字符
d,i int, decimal number
c int; single character


现在假设以下代码
Now suppose for the following code


#include< ; stdio.h中>
#include<stdio.h>


int main(void)

{

printf("%c", ''1''+ 1);

返回0;

}
int main(void)
{
printf("%c", ''1''+ 1);
return 0;
}


该程序的输出是2。
The output of the program is 2 .


因为''1'的整数值是49所以49 + 1 = 50,字符

相当于小数50是2。
Because the integer value of ''1'' is 49 so 49+1 =50 ,character
equivalent of decimal 50 is 2 .



差不多。 1和2的绝对值49和50不保证

(因为标准不需要ASCII),但ISO C _does_ demand

2紧跟在1之后。


Almost. The absolute values 49 and 50 for ''1'' and ''2'' are not guaranteed
(because the Standard doesn''t require ASCII), but ISO C _does_ demand
that ''2'' immediately follows ''1''.



这是否意味着上述程序的输出在非基于ASCII的系统的情况下可能不是2美元?

Does this mean that the output of the above program may be not 2 in
case of non ASCII based system ?


但我怀疑是
But my doubt is



问题。你的_question_是。


Question. Your _question_ is.


表达式1+ 1的类型是整数。所以,如果我使用%c来打印整数,它会显示未定义的行为吗?
The type of the expression ''1'' + 1 is integer. So if I use %c to
print the integer will it show undefined behavior?



从ISO C标准(1989年,碰巧,但C99是相同的):


#4.9。 6.1 fprintf函数

...

#c将int参数转换为unsigned char,并写入

#结果字符。


注意:_int_参数,转换为unsigned char。所以不,因为你认为它没有未定义的行为。


From the ISO C Standard (1989, as it happens, but C99 is the same):

# 4.9.6.1 The fprintf function
...
# c The int argument is converted to an unsigned char, and the
# resulting character is written.

Note: _int_ argument, which is converted to an unsigned char. So no, as
you thought, it does not have undefined behaviour.


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

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