C 以下程序的输出是什么?
当问题得到解决时运行该问题,终止循环C 以下程序的输出是什么?,c,loops,C,Loops,当问题得到解决时运行该问题,终止循环 #include<stdio.h> #include<conio.h> void main() { int n=12,res=1; clrscr(); while(n>3) { n+=3; res*=3; } printf("%d",n*res); getch(); } #包括 #包括 void main() { int n=12,res=1; clrsc(); 而(n>3) { n
#include<stdio.h>
#include<conio.h>
void main()
{
int n=12,res=1;
clrscr();
while(n>3)
{
n+=3;
res*=3;
}
printf("%d",n*res);
getch();
}
#包括
#包括
void main()
{
int n=12,res=1;
clrsc();
而(n>3)
{
n+=3;
res*=3;
}
printf(“%d”,n*res);
getch();
}
以上结果为3595。但我不明白while循环的过程。当它获取并终止其循环时?代码的行为是未定义的,因为您最终将溢出一个
有符号的
整数类型
因此,输出可以是任何东西
(在您的例子中,
n
似乎是一个负数,因此n>3
条件不再是true
。但是不要依赖此行为:例如,某些体系结构将钳制n
)学习。C不是C++。你的循环是错误的,n
总是优于3,直到它溢出为止!正如程序所说,它是无效的,任何关于它的行为的讨论都是没有意义的。