示例代码是(注释是什么我想它做的第一循环):为什么backspace在C中撤消一个单独的getchar()调用?
#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) ; ..意味着backspace几乎只是将光标移回而不删除任何东西,就像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();
??????????
}
}
什么我不得不穿上“____”,所以,当我的getchar再次调用分配给C,它前面分配到c后得到的焦炭,而不是之后的字符。