简体   繁体   English

以下程序片段的可能输出?

[英]Possible output of the following program fragment?

for(i=getchar();; i=getchar())
if(i=='x')
break;
else putchar(i);

Answer is : mi答案是:mi

Can someone explain this piece of code ?(MCQ Question)有人可以解释这段代码吗?(MCQ问题)

This question can be solved by eliminating incorrect answer.这个问题可以通过消除错误答案来解决。 This fragments prints character and exits loop if the character is an x .如果字符是x则此片段打印字符并退出循环。 So the program would not output an x .所以程序不会输出x

Any output string that doesn't contain x is possible.任何不包含x输出字符串都是可能的。 In your MCQ, possibly mi is the only option with x and all other options contain x somewhere in the string making them incorrect answer.在您的 MCQ 中,可能mix的唯一选项,所有其他选项在字符串中的某处都包含x ,使它们的答案不正确。

If input is "mix....", output would be "mi".如果输入是“mix....”,输出将是“mi”。 Below is your loop unrolled.下面是展开的循环。

getchar() -> m -> else -> print m  /* First getchar */
getchar() -> i -> else -> print i  /* Second getchar */
getchar() -> x -> if -> break      /* Second getchar */
for(i=getchar();; i=getchar())
if(i=='x')
break;
else putchar(i);

your Code will keep on running till it encounter 'x' so whatever input you give, it will read character by character as you have used getchar() function..您的代码将继续运行,直到遇到“x”,因此无论您提供什么输入,它都会在您使用 getchar() 函数时逐个字符地读取。

  • If character is 'x' then break the loop.如果字符是'x',则中断循环。
  • else print character.否则打印字符。

like, If the input is比如,如果输入是

sparx斯帕克斯

output will be输出将是

spar晶石

The for loop for 循环

 for(i=getchar();; i=getchar())

and syntax and structure of the for loop is for 循环的语法和结构是

for ( variable initialization; condition; variable update )

as i = getchar() will read char 'i' it is ok.因为 i = getchar() 将读取 char 'i' 没关系。 next there is no condition and final in updating you are again reading a character so it a infinite loop.接下来没有条件,最后更新你再次读取一个字符,所以它是一个无限循环。

Loop will terminate only when it will encounter 'x' as the statement只有当它遇到'x'作为语句时,循环才会终止

if(i=='x')
break;

Otherwise it will keep on printing the character.否则它将继续打印字符。

else putchar(i);

Here is the Demo .这是演示

Hope it helps!!希望能帮助到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以下程序的意外输出 - unexpected output of the following program 解释下面C++程序的output? - Explain the output of the following C++ program? 以下cpp程序的output如何证明合理 - How is the output for the following cpp program justified 为什么下面的程序在不使用互斥量的情况下没有混合output? - Why the following program does not mix the output when mutex is not used? 是否可以在程序运行时获得程序的输出? - Is it possible to get the output of a program while it's running? 为什么下面的 Hello World 程序在 PowerShell 上没有显示任何 output? 相同的程序在 CMD 上显示正确的 output - Why is the following Hello World program not showing any output on PowerShell ? The same program show correct output on CMD 是否可以检查将程序输出重定向到文件的控制台的宽度? - Is it possible to check width of a console in which the program output is redirected to a file? 使用这个应该输出最小可能整数的程序陷入无限循环 - Getting caught in an infinite loop with this program that is supposed to output the smallest possible integer 更新控制台输出,同时尽可能降低程序速度 - Update console output while slowing down program as little as possible 确定性C ++程序的“随机”输出。 可能的原因? - “Random” output from a determinstic C++ program. Possible causes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM