Understand backspace (\b) behaviour in C

后端 未结 2 1508
陌清茗
陌清茗 2020-12-10 05:28

This program copy its input to its output, replacing TAB(\\t) by \\t backspace(\\b) by \\b. But here in my code I am una

2条回答
  •  情歌与酒
    2020-12-10 06:07

    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
    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
    

提交回复
热议问题