【发布时间】:2015-11-08 02:09:59
【问题描述】:
示例代码是(cmets 是我想象它在第一个循环中所做的):
#include <stdio.h>
#define BACKSPACE 8
main()
{
int c;
while((c = getchar()) != EOF) //output: GODDAMN NOTHING cursor on: 'h'
{
//if the input is "house" before entering EOF
putchar(c); //output: 'h' cursor on: 'o'
getchar(); //output: 'h' cursor on: 'u'
printf("%c", BACKSPACE); //output: 'h' cursor on: 'o'
getchar(); //output: 'h' cursor on: 'u'
printf("%c", BACKSPACE); //output: 'h' cursor on: 'o'
getchar(); //output: 'h' cursor on: 'u'
printf("%c", BACKSPACE); //output: 'h' cursor on: 'o'
}
} //ACTUAL END OUTPUT: "h"
我知道大多数程序中的常规退格如下所示: printf("%c %c", 8 ,8); ..意思是退格几乎只是将光标向后移动而不删除任何内容,就像 getchar() 只是向前移动光标一样。
我试图理解为什么上面的示例代码的输出与以下内容不完全相同:
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF) //output: WE HAVE NOTHING cursor on: 'h'
{
//if the input is "house" before entering EOF
putchar(c); //output: 'h' cursor on: 'o'
}
} //ACTUAL END OUTPUT: "house"
编辑:跟进问题!如何“反转” getchar() 调用?
#include <stdio.h>
main()
{
int c;
char a;
while((c = getchar()) != EOF)
{
a = c;
putchar(c);
a = getchar();
??????????
}
}
我必须在“?????????之后。
【问题讨论】: