程序输出? [英] program output?

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

问题描述




我对C进行了测试。

有一个关于程序输出类型的客观问题。

以下是该计划:


main()

{

char ch;

int i = 2;

for(ch = 0; ch <128; ch ++)

i + = 2;

printf("%d" ,i);

}

i写的答案256将打印出来。

但我们的导师告诉我它将是无限循环。

是吗,我不确定答案。


问候,


Bhavik

Hi,

I took a test on C.
there was an objective question for program output type.
following is the program:

main()
{
char ch;
int i =2;
for (ch=0;ch<128;ch++)
i+=2;
printf("%d",i);
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.

Regards,

Bhavik

推荐答案

2004年5月24日04:20:00 -0700, bh ********** @ yahoo.com (bnp)写道:
On 24 May 2004 04:20:00 -0700, bh**********@yahoo.com (bnp) wrote:


我对C进行了测试。
对程序输出类型有一个客观的问题。
以下是程序:

main()
{
char ch;
int i = 2;
for(ch = 0; ch <128; ch ++)
i + = 2;
打印f(%d,i);
}
我写的答案256将被打印。
但是我们的导师告诉我它将是无限循环。
是那是对的,我不确定答案。


有点棘手。

首先,默认的签名是char类型是

平台依赖。如果它是8位和已签名,这是典型的基于PC的平台上的默认值,那么这确实会进入无限循环。

例如,8位字符的值范围是-128到+127。它b / b
总是小于+128。

如果chars的默认值是无符号的,那么程序输出是258 [I

没想出来,我只是跑了;-)]

-leor

问候,

Bhavik
Hi,

I took a test on C.
there was an objective question for program output type.
following is the program:

main()
{
char ch;
int i =2;
for (ch=0;ch<128;ch++)
i+=2;
printf("%d",i);
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.
Kinda tricky.
First of all, the default "signedness" of the char type is
platform-dependent. If it is 8 bits and "signed", as is the default on
typical PC-based platforms, then this does indeed go into an infinite loop.
In that case, the range of values for an 8-bit char is -128 to +127. It
will always be less than +128.

If the default for chars is unsigned, then the program output is 258 [I
didn''t figure it out, I just ran it ;-) ]
-leor

Regards,

Bhavik




-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


2004年5月24日星期一04:20:00 -0700,bnp写道:
On Mon, 24 May 2004 04:20:00 -0700, bnp wrote:


我对C进行了测试。
有一个关于程序输出类型的客观问题。
以下是程序:

main()


int main()或int main(void)是首选。

{


正确的缩进是一件好事(tm),即使对于诸如

这样的微不足道的例子也是如此。

char ch;
int i = 2;
for(ch = 0; ch< 128; ch ++)
i + = 2;
printf(" ;%d",i);
}
我写的答案256将被打印。
但我们的导师告诉我它将是无限循环。
是吗,是的,我不确定答案。
Hi,

I took a test on C.
there was an objective question for program output type.
following is the program:

main()
int main () or int main (void) is preferred.
{
Proper indentation is a Good Thing(tm), even for trivial examples such as
this.
char ch;
int i =2;
for (ch=0;ch<128;ch++)
i+=2;
printf("%d",i);
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.




你的导师可能是对的。这一切都取决于CHAR_MAX,如果char签名,可能是
为127。如果是这种情况,ch将永远不会是> = 128,

并且循环永远不会结束。如果CHAR_MAX大于127(因为它是
无符号,或者因为char有超过8个值位),那么它可以打印

258(并且将用于确定你是否打印换行符或刷新标准符号。


-

int main(void){int putchar(int),i = 0; unsigned long t = 500555079,n [] = {t

,159418370,88921539,286883974,80500161,0};而(n [i])putchar(*(!(t& 1) >
)+!(t ||!++ i)+"#\ n")),(t&& 1+(t>> = i-〜-i))|| (t = n [i] ^ n [i-1]);返回0;}



Your instructor may have been right. It all depends on CHAR_MAX, which may
be 127 if char is signed. If this is the case, ch will never be >= 128,
and the loop will never end. If CHAR_MAX is greater than 127 (because it''s
unsigned, or because char has more than 8 value bits), then it may print
258 (and will for sure if you print a newline or flush stdout).

--
int main(void){int putchar(int),i=0;unsigned long t=500555079,n[]={t
,159418370,88921539,286883974,80500161,0};while(n[i])putchar(*(!(t&1
)+!(t||!++i)+"# \n")),(t&&1+(t>>=i-~-i))||(t=n[i]^n[i-1]);return 0;}




星期一, 2004年5月24日,bnp写道:

On Mon, 24 May 2004, bnp wrote:

我对C进行了测试。
有一个关于程序输出类型的客观问题。
以下是程序:



好​​的风格要求包含< stdio.h> (见下面有关

''printf''的错误。)

main()


_int_ main()需要C99 ;

C89和C99中_int_ main(_void_)首选。 (其中_this_表示强调,而不是文字下划线。)

{
char ch;


缺少缩进是一件坏事,应该避免,尤其是在Usenet和学校测试中使用
。此外,其他地方。

int i = 2;
for(ch = 0; ch< 128; ch ++)


非严格 - 一致性:程序行为取决于

普通字符的签名。

可能的未定义行为:如果char已签名且有8位,则

有符号整数溢出将发生。

i + = 2;
printf("%d",i);


可能的链接器错误:''printf''未定义。

未定义的行为:variadic函数''printf''调用没有

范围内的原型。

约束违规(这是正确的吗?):程序输出结束

而不终止换行''\ n''。

}
我写了答案256将被打印。
但我们的导师告诉我它将是无限循环。
是吗,我不确定答案。

I took a test on C.
there was an objective question for program output type.
following is the program:


Good style dictates the inclusion of <stdio.h> (see errors regarding
''printf'' below).
main()
_int_ main() required for C99; _int_ main(_void_) preferred in both
C89 and C99. (Where _this_ indicates emphasis, not literal underscores.)
{
char ch;
Lack of indentation is a bad thing and should be avoided, especially
on Usenet and on school tests. Also, everywhere else.
int i =2;
for (ch=0;ch<128;ch++)
Non-strict-conformance: program behavior depends on signedness of
plain char.
Possible undefined behavior: If char is signed and has eight bits,
signed integer overflow will occur.
i+=2;
printf("%d",i);
Possible linker error: ''printf'' undefined.
Undefined behavior: variadic function ''printf'' called without
prototype in scope.
Constraint violation (is that correct?): program output ends
without terminating newline ''\n''.
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.




是的,你们两个都是正确的。只有程序错了。


-Arthur



Yes, you are both "right." Only the program is wrong.

-Arthur


这篇关于程序输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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