Understand backspace (\\b) behaviour in C

£可爱£侵袭症+ 提交于 2019-11-28 10:04:42

The backspace is consumed by the shell interpreter, so your program will never see it, also your code is (slightly) broken, due to misplaced braces, not helped by the poor indentation.

Here is a corrected version:

#include<stdio.h>
int main(void)
{
    int c=0;
    while((c=getchar())!=EOF){
        if(c=='\t')
            printf("\\t");
        else if(c=='\b')
            printf("\\b");
        else
            putchar(c);
    }
    putchar('\n');
    return 0;
}

which works as expected:

$ echo 'vinay\thunachyal\b' | ./escape
vinay\thunachyal\b

If I haven't misinterpreted the question, you may use 'Ctrl-H' to send a backspace. Using trojanfoe's corrected code, when you type:

vinay^H

It will print:

vinay\b

^H means 'Ctrl-H', it's ASCII character #8, which is backspace.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!